👉 staging area에 들어가 있는(add가 된 파일에 한해서) 작업해놓은 branch를 숨겨 놓을 수 있다!
👇 명령어들
$ git stash
$ git stash apply
$ git stash drop
$ git stash pop
$ git stash list
🍕코드
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (main)
$ vim s1.txt
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (main)
$ git add s1.txt
warning: LF will be replaced by CRLF in s1.txt.
The file will have its original line endings in your working directory
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (main)
$ git commit -m "stash"
[main 43bba6a] stash
1 file changed, 1 insertion(+)
create mode 100644 s1.txt
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (main)
$ git checkout -b test
Switched to a new branch 'test'
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ vim s1.txt
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git status
On branch test
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: s1.txt
no changes added to commit (use "git add" and/or "git commit -a")
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git checkout main
Switched to branch 'main'
M s1.txt
Your branch is ahead of 'origin/main' by 3 commits.
(use "git push" to publish your local commits)
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (main)
$ git status
On branch main
Your branch is ahead of 'origin/main' 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 restore <file>..." to discard changes in working directory)
modified: s1.txt
no changes added to commit (use "git add" and/or "git commit -a")
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (main)
$ git checkout test
Switched to branch 'test'
M s1.txt
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git stash
warning: LF will be replaced by CRLF in s1.txt.
The file will have its original line endings in your working directory
Saved working directory and index state WIP on test: 43bba6a stash
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git status
On branch test
nothing to commit, working tree clean
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git checkout main
Switched to branch 'main'
Your branch is ahead of 'origin/main' by 3 commits.
(use "git push" to publish your local commits)
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (main)
$ git status
On branch main
Your branch is ahead of 'origin/main' by 3 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
🍔설명
1) main에서 s1.txt라는 파일을 생성 및 커밋
2) git checkout -b test
➡ test라는 branch를 생성과 동시에 checkout
3) vim s1.txt ➡ test에서 s1.txt 수정
4) git status ➡ "modified: s1.txt"
5) git chekcout main
6) git status ➡ "modified:s1.txt"
👉bran에서 s1.txt를 수정한건데 main까지 영향을 준다. 이럴 때 쓰는게 바로 ✨stash✨
7) git checkout test
8) git stash
➡ Saved working directory and index state WIP on test: 43bba6a stash
9) git status
➡ nothing to commit 👉작업중이던 f1.txt가 숨겨진다.
10)git checkout main
👉main으로 돌아가 다른 작업과 커밋 완료 후 다시 숨겨놨던 s1.txt를 꺼내면 된다
🍟코드
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (main)
$ git checkout test
Switched to branch 'test'
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git stash list
stash@{0}: WIP on test: 43bba6a stash
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git stash apply
On branch test
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: s1.txt
no changes added to commit (use "git add" and/or "git commit -a")
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git status
On branch test
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: s1.txt
no changes added to commit (use "git add" and/or "git commit -a")
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git reset --hard HEAD
HEAD is now at 43bba6a stash
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git status
On branch test
nothing to commit, working tree clean
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git stash list
stash@{0}: WIP on test: 43bba6a stash
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git stash apply
On branch test
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: s1.txt
no changes added to commit (use "git add" and/or "git commit -a")
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git status
On branch test
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: s1.txt
no changes added to commit (use "git add" and/or "git commit -a")
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git reset --hard
HEAD is now at 43bba6a stash
🌭설명
1) git checkout -b test
➡ test로 checkout
2) git stash list
➡ stash@{0}: WIP on test: 43bba6a stash
👉 stash한 목록을 보여줌
3) git stash apply
👉숨겨놨던 수정된 s1.txt가 다시 나타남
4) git status
➡"modified: s1.txt"
5) git reset --hard HEAD
👉가장 최근 커밋 상태로 리셋 시켜줌
6) git status
➡ nothing to commit, working tree clean
👉reset 했기 때문에 s1.txt 수정 사항이 안보임.
그렇다면 수정한 부분도 사라진걸까? ▶ 답은 NO‼
7) git stash list
➡ stash@{0}: WIP on test: 43bba6a stash
👉stash list에도 여전히 존재함!
8) git stash apply
👉다시 stash한 항목 되돌리기
9) git status
➡ modified: s1.txt
👉 reset해서 nothing to commit이었다가 다시 modified:s1.txt로 돌아갔다.
stash list에 있던 수정 항목이 그대로 다시 나타난다.
10) git reset --hard
👉 다시 최근 커밋상태로 되돌리기
🥞코드
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git status
On branch test
nothing to commit, working tree clean
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ vim s1.txt
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git stash
Saved working directory and index state WIP on test: 43bba6a stash
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git stash list
stash@{0}: WIP on test: 43bba6a stash
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ vim s2.txt
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git add s2.txt
warning: LF will be replaced by CRLF in s2.txt.
The file will have its original line endings in your working directory
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git status
On branch test
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: s2.txt
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git stash
Saved working directory and index state WIP on test: 43bba6a stash
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git status
On branch test
nothing to commit, working tree clean
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git stash list
stash@{0}: WIP on test: 43bba6a stash
stash@{1}: WIP on test: 43bba6a stash
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git stash apply; git stash drop;
On branch test
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: s2.txt
Dropped refs/stash@{0} (1bb1718d077f20e8dd248e01bfccc57e36e5fecb)
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git stash list
stash@{0}: WIP on test: 43bba6a stash
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git stash apply;
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: s1.txt
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git stash list
stash@{0}: WIP on test: 43bba6a stash
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git stash apply; git stash drop;
On branch test
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: s1.txt
Dropped refs/stash@{0} (1bb1718d077f20e8dd248e01bfccc57e36e5fecb)
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git stash list
➕ 추가!
ssnhh@DESKTOP-9APISNT MINGW64 ~/git/forGit (test)
$ git stash pop
On branch test
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: s1.txt
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (57c44e069dd6b7c4f46dc6de0ea1e5776ff56098)
🥨설명
1) $ vim s1.txt
$ git stash
👉 s1.txt 수정 후 stash 처리
2) $ git stash list
➡ stash@{0}: WIP on test: 43bba6a stash
3) $ vim s2.txt
$ git add s2.txt
$ git stash
👉 s2.txt 생성 후 add & stash
👉 이로써 수정한 s1.txt , 새로 생성한 s2.txt stash 처리한 항목은 2개가 됨
4) $ git stash list
➡ stash@{0}: WIP on test: 43bba6a stash
➡ stash@{1}: WIP on test: 43bba6a stash
👉 제일 최근에 stash 시킨게 list 최상단에 위치, stash 실행한 역순으로 정렬
👉 stash@{0}: WIP on test: 43bba6a stash ▶ s2.txt
stash@{1}: WIP on test: 43bba6a stash ▶ s1.txt
5) $ git stash apply; git stash drop;
➡ On branch test
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: s2.txt
Dropped refs/stash@{0} (1bb1718d077f20e8dd248e01bfccc57e36e5fecb)
👉 제일 최상단에 있는 stash@{0}이 apply 됨. ▶ new file: s2.txt
👉 apply 후 list에서 drop
👉 apply만 실행하면 list에 이미 stash한 항목이 그대로 남아있다.
다음 stash항목도 실행해야 하므로 drop명령어를 써 최상단 목록을 삭제해준다.
6) $ git stash list
➡ stash@{0}: WIP on test: 43bba6a stash
7) $ git stash apply;
➡ On branch test
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: s1.txt
8) $ git stash list
➡ stash@{0}: WIP on test: 43bba6a stash
👉 이렇게 list에 그대로 남아있음
9) $ git stash drop
➡ Dropped refs/stash@{0} (1bb1718d077f20e8dd248e01bfccc57e36e5fecb)
10) $ git stash list
👉 실행하면 아무것도 안나온다!
➕ $ git stash pop
➡ $ git stash apply; git stash drop;
👉 pop을 사용하면 apply&drop이 한번에 된다!
📌작업 동기화를 막기 위해 stash 처리를 한다.
stash를 하면 하던 작업을 숨김 처리 가능 ▶ 단, reset을 하여 이전 커밋버전으로 돌아간다 해도 stash 처리한 항목이 없어지지는 않음!