여러 작업을 병행하기 위해서 깃 브랜치를 이용하여 작업하게 되는데, 코드 작업 중 급하게 다른 작업을 처리해야 하거나 현재 작업을 커밋하지 않고 보관하기 위한 방법으로 git stash가 있다.
깃을 처음 다루면서 add, push, commit만 사용하고 stash는 잘 이해도 안 되고 사용하는 방법이 어려운 것 같아서 잘 사용하지 않다가 제대로 익히고 사용하니 너무 편리하다.
Git Stash란?
변경사항을 임시로 저장하는 기능이다. 저장된 변경 사항은 스태시라는 특별한 영역에 보관되며, 필요할 때 다시 불러올 수 있다. 이렇게 함으로써 현재 작업을 중단하고 다른 작업을 하더라도, 나중에 다시 원래 작업으로 돌아갈 수 있다.
스태시 활용 예시
1) 긴급한 버그 수정: 기능을 개발하던 도중 긴급한 버그 수정 요청이 들어왔을 때, 현재 작업을 커밋하지 않고도 안전하게 저장하고, 다른 브랜치로 이동하여 버그를 수정할 수 있습니다.
2) 여러 기능 병행 개발: 여러 기능을 동시에 개발해야 할 때, 각 기능의 진행 상태를 스태시로 저장하고 필요한 기능으로 쉽게 전환할 수 있습니다.
3) 코드 리뷰 준비: 코드 리뷰를 위해 클린한 상태로 브랜치를 만들어야 할 때, 작업 중인 내용을 스태시로 저장해 두고 리뷰를 준비할 수 있습니다.
Git Stash 사용법
변경 사항을 스태시로 저장하기
현재 작업 디렉토리의 모든 변경사항이 스태시로 저장이 된다. 변경사항이 저장된 후, 작업 디렉터리는 마지막 커밋 상태로 돌아간다.
-u 옵션을 붙인 이유는 추적되지 않은 파일도 함께 스태시로 저장하기 위해서 붙여주었다. 추가되지 않은 파일이라 함은 git status로 확인하였을 때 "?"로 표시되는 파일들이다.
git stash -u
저장된 스태시 목록 보기
스태시 된 모든 항목을 볼 수 있다. 스태시 항목은 'stash@{n}' 과 같이 나오며 n 대신 0, 1, 2, 3 이 들어간다
git stash list
작업하기
스태시를 저장하고 목록까지 제대로 확인했다면, 이제 필요한 작업을 하면 된다. 브랜치를 옮겨도 되고 다른 에러 처리를 해도 된다.
스태시 적용하기
가장 최근의 스태시를 적용하여, 작업 디렉터리의 변경사항을 가지고 온다. 옵션으로 'stash@{n}'을 줌으로써 특정 스태시를 가지고 올 수 있다.
# 스태시 적용하기
git stash apply
git stash apply stash@{n}
# 스태시 적용과 동시에 삭제하기
git stash pop
git stash pop stash@{n}
스태시 삭제하기
pop을 통해서 적용과 동시에 삭제하였다면 상관이 없지만 apply를 통해서 적용시켜 줬다면 아래의 명령어를 통해서 스태시를 삭제할 수 있다.
# 특정 스태시 삭제
git stash drop stash@{n}
# 모든 스태시 삭제
git stash clear
'GIT' 카테고리의 다른 글
[Git] 푸시전!! 커밋 메시지 남긴 후, 파일이 수정되었다면? (0) | 2024.08.29 |
---|---|
[GIT] 깃허브 자격 인증 데이터 변경 시, access token 재 등록하기 (0) | 2024.08.08 |
[git] git repository 합치기 - commit 기록 남기기 (0) | 2024.07.18 |
[GIT] Git 캐시(Cache) 삭제 (0) | 2024.07.03 |
[GIT] Pre-commit 사용법 및 설정 가이드 (0) | 2024.05.17 |