본문 바로가기

git/github

[Git] stash 사용 예


stash 명령어는 워킹 디렉토리에서 수정한 파일들만 저장이 된다. 수정내용을 잠시 스택에 저장해둔다고 생각하면 간단하다.


git repository에서 이미 커밋되어있던 3개의 파일을 수정하여 작업을 하고있다.


1. vi a.txt

2. vi b.txt

3. vi c.txt


a.txt는 스테이지모드로 변경


4. git add a.txt


git status 확인

$ git status

# On branch master

#

# Initial commit

#

# Changes to be committed:

#   (use "git rm --cached <file>..." to unstage)

#

#       new file:   a.txt

#

# Untracked files:

#   (use "git add <file>..." to include in what will be committed)

#

#       b.txt

#       c.txt


이 상태에서 급한 수정 요청이 들어온다. 하지만, 지금까지 작업했던 내용을 커밋하기는 좀 껄끄럽다.. 아직 완료가 된 상황은 아니라서.. 어딘가에 이 작업한 내용을 잠시 저장해두었다가 급한 수정을 처리하고 다시 작업을 이어 할 수 없을까?


5. git stash

$ git stash


6. git status
$ git status
# On branch master
nothing to commit, working directory clean

이제 들어온 작업을하자. 그리고 작업이 끝나고 다시 아까 작업하던 것을 불러오고 싶다.

7. git stash list
$ git stash list
stash@{0}: WIP on master: 2860425 t
stash@{1}: WIP on master: be232f7 init

stash의 리스트가 조회된다. 위에 있는 것이 가장 최근에 저장한 stash이다. 이걸 다시 적용해보자.

8. git stash apply
$ git stash apply
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   a.txt
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   b.txt
#       modified:   c.txt
#

progit 책에는 apply만 실행하면 staged 상태의 파일이었던 것들은 자동으로 staged상태로 만들어주지 않는다고 되어있는데.. 테스트한 1.8.X 버전에서는 자동으로 만들어주는것 같다. 그리고 단순히 git stash apply를 실행하면 가장 최근의 stash가 적용이 되며, 다른 stash를 적용하고 싶을때는 stash 이름을 입력하면 가능하다.


아무래도 많이 사용하게 될것 같아서... 잠깐 정리를..