git stash 干嘛滴
假如你正在开发feature-01,但是依赖的一个模块代码修改了,你必须pull代码,然而你又不想commit,这时可以使用git stash
来保存当前未commit的代码更改。
实例
假设仓库已经有stash-01.txt空文件,vim stash-01.txt,你加了一行文本:
I am developing feature-01 |
这时同学A说,你必须pull代码,然后你微笑着用git stash
保存当前未commit的代码:
$ git stash
Saved working directory and index state WIP on develop: 80ccebf add stash file
HEAD is now at 80ccebf add stash file
之后git status
再次确认了当前状态是干净的:
$ git st |
这时候可以放心的pull了:
$ git pull |
这个pull操作可能修改了多个文件,很可能涉及到你刚才stash的文件,这时会发生什么呢?我们接着看:
$ git stash pop |
Ohhh,冲突了,这是当然的啦。就算是commit之后再pull也是会冲突,只要merge一下就好了。
这时你检查了一下,发现其实已经完成feature-01了,所以commit and push了。
之后,你开始了feature-02的开发,同样你加了一行:
common lib modified |
这时,同学A又让你pull代码,所以你又再次微笑着:
$ git stash
Saved working directory and index state WIP on develop: 425394b feature-01 done
HEAD is now at 425394b feature-01 done
$ git pull
$ git stash pop
$ git merge
这时候你被打断,没有commit feature-02,后来继续开发feature-03了,接着第三次pull来了:
$ git stash
Saved working directory and index state WIP on develop: 7ed8d8d 2th, common lib2 modified
HEAD is now at 7ed8d8d 2th, common lib2 modified
$ git pull |
上图, 可以发现,刚才未提交的merge结果(同学A竟然把2nd写错了⊙﹏⊙),相同的位置现在需要重新merge,这时赶紧commit & push。
$ git commit -m "Add: feature-02 and 03 done"
如果刚才不是同一个文件,可以stash多次,这个堆栈可以这样查看:
$ git stash list |
发现pop出来的stash还存在着,还可以切回去某一次stash:
$ git stash apply stash@{0}
当然,你可能面临冲突,谨慎使用,看场景~~
#