命令:stash
把工作空间的修改存放起来,从而保持工作空间的clean,完成操作后再恢复。
场景:
- 写了一些东西了,突然需要切换到其他分支(比如打个包?)
- 本地有一些文件不能提交,也不方便写在.gitignore中,这个时候想用rebase的方式来pull代码(避免merge时产生的commit)
你当然可以写一个commit,然后再reset;但使用stash(存放)是更合适的。
用法:
- 存放本地修改
git stash
- 还原之前存放的修改
git stash pop
场景Detail:
切换分支的场景就不说了,很常规。
下面说一下在本地修改未提交的情况下,如何使用rebase方式pull更新
有些人不喜欢merge产生的commit,会给git时间线增加很多分叉,于是人们使用rebase来合并commit.
如果你的操作严格地符合git规范,那么这个场景很正常,就没啥可说的了。可在实际开发中,我们常常会有这些的问题:
一些本地文件不能提交,也不方便加入.gitignore中
一些修改也不能提交,于是在本次修改commit后,工作空间会留下一些的东西。

这个时候你要提交了,先更新代码,于是你使用
git pull --rebase
(为啥用rebase前面已经说了),看到下面的错误:

这个时候只需要存放一下,

发现工作空间已经clean了:

这个时候,就可以愉快地rebase了

最后,恢复存放的数据:

更多内容,参考doc
网友评论