Git Stash
참고
Stash
수정/삭제/변경했지만, 아직 commit 하지 않은 변경 사항을 일시적으로 임시 저장하는 기능
아직 commit 하기에는 이른 경우나, 다른 branch로 checkout 하는 경우 변경 사항을 유지하고 싶을 때 사용 가능
현재 수정본은 전부 저장되면서 마지막으로 commit한 상태로 코드 복원
다른 브랜치에 작업해야 했지만 실수로 현재 브랜치에서 작업한 경우 등에 사용
파일 수정 내용이 많은데 디버깅이 힘든 경우 등 여러 상황에서 이용
현재 수정본은 전부 저장되면서 마지막 commit 상태로 코드 복원
git stash
현재 작 중인 변경 사항 일시적으로 저장하며, 저장 변경된 것은 stack에 쌓임
git stash
git statsh save
git stash list
현재 stash 목록 확인
git stash list
git pop
Stack에 쌓인 가장 최근의 변경 사항을 불러와 작업 디렉토리에 적용
해당 변경 사항 제거
git stash pop
Git Stash 예시
master라는 remote branch는 local의 master branch에서 push
kkkk 라는 local branch에서 Readme.md 파일을 수정한 후 kkkk라는 remote branch로 commit 후 push
kkkk remote branch를 remote master branch에 pull request한 후 merge
이 때 local master branch에서 Readme.md 파일을 수정하고 git pull을 하게 되면
error: Your local changes to the following files would be overwritten by merge:
README.md
Please commit your changes or stash them before you merge.
Aborting
위와 같은 에러가 발생
이때, 이를 해결하기 위해서 다음과 같은 방법 이용
git stash
-
git stash
를 하게 되면 마지막 commit 상태로 코드 복원 -
아래와 같은 결과 나옴
Saved working directory and index state WIP on master: a546103 Merge pull request #1 from YJH-jm/abc
git stash의 목록들을 확인
git stash list
-
목록들을 아래와 같이 확인 가능
stash@{0}: WIP on master: a546103 Merge pull request #1 from YJH-jm/abc
그 후 git pull
진행하여 remote master branch의 변경 사항을 local에 적용
stash pop
적용하고 변경 사항을 확인하고 저장하고 다시 commit, push 적용
다른 경우는 abc라는 branch에서 작업을 하다 commit을 하기 전 kkkk라는 branch로 이동하면
Please commit your changes or stash them before you switch branches.
Aborting
라는 에러가 발생
이 때 git stash
를 사용