文章目录
git stash
git stash
命令可临时保存和恢复被git跟踪的 工作区 or 暂存区 的修改,可跨分支。
注:未被git
跟踪的修改是不能被git stash
临时保存
git stash list
developmac@DevelopdeMBP test % git log --oneline
6588dc0 (HEAD -> master) init repository
developmac@DevelopdeMBP test % git stash list
stash@{0}: WIP on master: 6588dc0 init repository
查看保存的所有记录列表
git stash push
developmac@DevelopdeMBP test % git status
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: b.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
no changes added to commit (use "git add" and/or "git commit -a")
developmac@DevelopdeMBP test % git stash
Saved working directory and index state WIP on master: 6588dc0 init repository
developmac@DevelopdeMBP test % git stash list
stash@{0}: WIP on master: 6588dc0 init repository
stash@{1}: WIP on master: 6588dc0 init repository
developmac@DevelopdeMBP test % git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
nothing added to commit but untracked files present (use "git add" to track)
developmac@DevelopdeMBP test % git stash pop
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: b.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (0ac74d384d4f7c039577c3582006d6f2bb85f870)
developmac@DevelopdeMBP test % git stash list
stash@{0}: WIP on master: 6588dc0 init repository
developmac@DevelopdeMBP test % git stash push
Saved working directory and index state WIP on master: 6588dc0 init repository
developmac@DevelopdeMBP test % git stash pop
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: b.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (d4da6d3a1059c686d92dc53ba12d185a21b5e640)
developmac@DevelopdeMBP test % git stash push -m
error: switch `m' requires a value
developmac@DevelopdeMBP test % git stash push -m 'b.txt stash'
Saved working directory and index state On master: b.txt stash
developmac@DevelopdeMBP test % git stash list
stash@{0}: On master: b.txt stash
stash@{1}: WIP on master: 6588dc0 init repository
.
.
.
developmac@DevelopdeMBP test % git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: a.txt
new file: c.txt
developmac@DevelopdeMBP test % git stash list
stash@{0}: On master: b.txt stash
stash@{1}: WIP on master: 6588dc0 init repository
developmac@DevelopdeMBP test % git stash push a.txt -m 'a.txt stash'
Saved working directory and index state On master: a.txt stash
developmac@DevelopdeMBP test % git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: c.txt
developmac@DevelopdeMBP test % git stash list
stash@{0}: On master: a.txt stash
stash@{1}: On master: b.txt stash
stash@{2}: WIP on master: 6588dc0 init repository
上面的WIP意思是work in process,即工作中,从以上例子可以看到:
git stash
=git stash push
将当前受git
跟踪****仍在工作区和暂存区中的所有修改临时保存。不指定message,默认stash message
为
WIP on <分支名>: <git log中最新log的hashcode> <git log中最新log的message>
git stash push -m 'message'
将当前受git
跟踪仍在工作区和暂存区中的所有修改临时保存。指定message,stash记录格式为
stash@{0}: On <分支名>: <message>
git stash push <文件1> <文件2> ... -m 'message'
git stash push
后面跟文件列表可以只存指定的文件,而非所有跟踪修改的文件;
还可看到,git stash list
显示的保存列表是倒序的,即stash@{num}
中的num越小,保存记录越近,就像栈,后进先出。
git stash clear
删除所有的stash
保存记录。
git stash drop stash@{num}
删除stash@{num}
的stash
保存记录。
git stash pop stash@{num}
使用stash@{num}
保存记录恢复工作区,只能恢复一次,因为pop后,这个stash@{num}
保存记录就会出栈。
git stash pop
可以省略stash@{num}
,默认是栈顶记录,即stash@{0}
保存记录。
git stash apply stash@{num}
使用stash@{num}保存记录恢复工作区,与pop不同的是可以恢复多次,因为stash@{num}保存记录不会出栈。
同样可以省略stash@{num},默认是栈顶记录,即stash@{0}保存记录。
参考: