git stash

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,即工作中,从以上例子可以看到:

  1. git stash = git stash push

将当前git跟踪****仍在工作区和暂存区中的所有修改临时保存。不指定message,默认stash message

WIP on <分支名>: <git log中最新log的hashcode> <git log中最新log的message>
  1. git stash push -m 'message'

将当前受git跟踪仍在工作区和暂存区中的所有修改临时保存。指定message,stash记录格式为

stash@{0}: On <分支名>: <message>
  1. 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}保存记录。


参考:

git之stash

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值