아래 내용은 Pluralsight의 Master Git, 누구나 이해할 수 있는 Git 입문, 생활코딩 지옥에서 온 Git의 내용을 토대로 정리한 내용입니다.
1. git stash
- 작업 도중 커밋을 할 수는 없는 상태지만, 브랜치 전환을 하거나 커밋 변경을 해야할때 임시적으로 저장할 수 있는 명령어
commit
되기 이전 작업 중 파일과git add
로 추가된 파일 까지 모두 stash 영역으로 저장되며 수정분량은 제거 된다.- 단
git add
명령어로 한번이라도 index 영역에 트래킹 된 파일만 stash영역으로 저장됨 - 새롭게 생성한 파일은
git add
후git stash
명령어를 사용하거나git stash -u
옵션을 같이 사용할 것
2. git stash 기본적인 사용법
git stash
:- git add` 명령어로 한번이라도 index 영역에 트래킹 된 파일을 기본 명칭으로 stash 임시 영역에 저장되고, 수정 분량은 제거됨
- 기본 명칭
WIP
로 저장됨
git stash -u
:- 새롭게 추가한 파일도 함께 stash 영역에 저장
git stash save 명칭
git stash
로 저장할 때 명칭을 주어 저장함
git stash list
- stash 한 기록을 리스트형태로 확인할수 있다.
- 명시적으로 삭제하지 않으면 남아 있음
git stash apply
- 가장 최근에 저장한 stash를 복원 한다.
git stash apply stash아이디
stash아이디
는git stash list
명령어를 실행하면 리스트 앞에stash@{0}
이렇게 개별적인 id 값이 있는데 이를 적용한다.stash아이디
의 임시 저장이 복원된다.
git stash drop
- 가장 최근에 저장한 stash를 삭제 한다.
git stash drop stash아이디
stash아이디
의 리스트가 삭제된다.
git stash clear
- stash 기록이 모두 제거된다.
git stash pop
- stash를 복원하고 바로 제거 된다.
git stash branch
- Stash 할 당시의 커밋을 Checkout 한 후 새로운 브랜치를 만들고 여기에 적용 후 Stash를 제거
3. git stash 기본적인 사용 예제
- 작업 도중의 파일이 있는 상태(
git status
명령어로 확인) - 수정파일 4개 그 중 1개는
git add
명령어로 index 영역에 존재, 그리고 1개는 새롭개 추가한 파일
3.1 git status 파일 상태 확인
$ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: menu.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: recipes/README.txt
modified: recipes/apple_pie.txt
modified: recipes/spaghetti_alla_carbonara.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
test3.txt
3.2 git stash 옵션 없이 사용
git stash
옵션 없이 사용
$ git stash
Saved working directory and index state WIP on master: fbe5356 Well, I changed my mind about that renaming
HEAD is now at fbe5356 Well, I changed my mind about that renaming
git status
로 확인test3.txt
새롭게 추가된 파일을 제외하고 stash 영역에 저장되며 수정 되거나 추가된 부분은 제거됨
$ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
(use "git push" to publish your local commits)
Untracked files:
(use "git add <file>..." to include in what will be committed)
test3.txt
nothing added to commit but untracked files present (use "git add" to track)
3.3 git stash list 로 stash 기록 확인
git stash list
명령어로 stash 기록 확인
$ git stash list
stash@{0}: WIP on master: fbe5356 Well, I changed my mind about that renaming
3.4 git stash apply 로 수정 코드 복원 적용
git stash apply
명령어로 최신 임시 저장 분량 복원
$ git stash apply
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
(use "git push" to publish your local commits)
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: menu.txt
modified: recipes/README.txt
modified: recipes/apple_pie.txt
modified: recipes/spaghetti_alla_carbonara.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
test3.txt
no changes added to commit (use "git add" and/or "git commit -a")
3.5 git stash drop stash아이디로 stash 제거
git stash drop
명령어로 제거
$ git stash drop stash@{0}`
git stash list
확인
$ git stash list