[Git] stash

업데이트:     Updated:

카테고리:

태그:

🎯stash

stash는 Modified이면서 Tracked 상태인 파일과 Staging Area에 있는 파일들을 보관해두는 장소다. 아직 끝내지 않은 수정사항을 스택에 잠시 저장했다가 나중에 다시 적용하는데 사용할 수 있다.

  • 현재 브랜치에서 대상 브랜치로 Checkout할 때 충돌되는 파일이 없는 경우에는 변경된 내용이 대상 브랜치로 모두 따라온다.
  • 현재 브랜치에서 대상 브랜치로 Cechkout할 때 충될되는 파일이 있는 경우에는 변경된 내용을 stash 하거나 commit 해야 한다.

예제 프로젝트

예제 프로젝트를 하나 살펴보자. 파일을 두 개 수정하고 그 중 하나는 Staging Area에 추가한다. 그리고 git status 명령을 실행하면 아래와 같은 결과를 볼 수 있다.

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   index.html

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   lib/simplegit.rb

stash 하기

작업을 하던 중에 급하게 처리할 이슈가 생겨 현재 작업 중인 파일을 커밋하지 않고 잠시 stash 하고 브랜치를 변경한다고 가정해보자. git stashgit stash save 명령을 실행하면 스택에 새로운 Stash가 만들어지고 워킹 디렉토리와 Staging Area는 clean 된다.

$ git stash 
Saved working directory and index state WIP on master: 652247d First Commit
$ git status
On branch master
nothing to commit, working tree clean

stash list

git stash 명령을 이용해 수정하던 파일을 스택에 저장했다. 저장된 Stash를 확인하려면 git stash list 명령을 사용한다.

$ git stash list
stash@{0}: WIP on master: 652247d First Commit

stash apply

git stash apply 명령을 사용하여 Stash를 다시 적용할 수 있다. git stash apply stash@{0} 처럼 Stash 이름을 지정하여 적용할 수 있다. 이름이 없으면 Git은 가장 최근의 Stash를 적용한다.

$ git stash apply
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   index.html
        modified:   lib/simplegit.rb

no changes added to commit (use "git add" and/or "git commit -a")
  • Git은 Stash에 저장할 때 수정했던 파일들을 복원해준다. 어떤 브랜치에서 Stash 하고 다른 브랜치로 옮기고서 거기에 Stash를 복원할 수도 있다.
  • Git은 Stash를 적용할 때 Staged 상태였던 파일을 자동으로 다시 Staged 상태로 만들어 주지 않는다. 그래서 git stash apply 명령을 실행할 때 --index 옵션을 주어 Staged 상태까지 적용할 수 있다.

stash drop

apply 명령은 단순히 Stash를 적용하는 것뿐이다. Stash는 여전히 스택에 남아 있다. git stash drop 명령을 사용하여 해당 Stash를 제거할 수 있다.

$ git stash list
stash@{0}: WIP on master: 652247d First Commit
$ git stash drop stash@{0}
Dropped stash@{0} (43a4409c7ebad88e45474228505d87eb0d97e545)
$ git stash list

그리고 git stash pop 이라는 명령도 있는데 이 명령은 Stash를 적용하고 나서 바로 스택에서 제거해준다.

Stash를 만드는 다른 방법

  1. git stash 명령에 --keep-index 옵션을 이용하면 Staging Area에 있는 파일은 Stash 하지 않는다.
  2. git stash 명령에 --include-untracked-u 옵션을 붙여주면 Untracked 파일을 같이 Stash 할 수 있다. (기본적으로 git stash 명령은 tracked 상태의 파일만 저장한다.)
  3. git stash 명령에 --patch 옵션을 붙이면 Git은 수정된 모든 사항을 저장하지 않는다. 대신 대화형 프롬프트가 뜨며 변경된 데이터 중 저장할 것과 저장하지 않을 것을 지정할 수 있다.

Stash를 적용할 브랜치 만들기

git stash branch <브랜치 이름> 명령을 실행하면 Stash 할 당시의 커밋을 Checkout 한 후에 새로운 브랜치를 만들고 여기에 적용한다. 이 모든 것이 성공하면 Stash를 삭제한다.

$ git stash branch testbranch
Switched to a new branch 'testbranch'
On branch testbranch
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   index.html

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   lib/simplegit.rb

Dropped refs/stash@{0} (6cac35a9769512aa21f09f404ef8127ca418e97a)

📌출처

Git Docs


👍 개인 공부 기록용 블로그입니다. 오류나 조언이 있으시면 언제든지 댓글 혹은 메일로 남겨주시면 감사하겠습니다! 😄

맨 위로 이동하기

git 카테고리 내 다른 글 보러가기

댓글남기기