程序员

关于git stash命令

2014-03-06  本文已影响215人  aurora

今天完成了一个新功能准备提交到服务器的git仓库上,发现有同事更新了项目,但是我的本地git仓库的项目代码不是最新版本,为了避免出现版本冲突,所以Google了一下解决方案,见文末的参考资料
主要是用到了git stash命令,此命令的应用场景是:本地代码发生了改动,但是不想提交到代码仓库里,于是把这部分改动暂存起来。
于是我执行了

$ git stash
Saved working directory and index state "WIP on master: 049d078 added the index file" HEAD is now at 049d078 added the index file (To restore them type "git stash apply")

这时本地项目恢复到了改动之前的版本,执行

$ git pull
Merge made by the 'recursive' strategy. ...... 11 files changed, 249 insertions(+), 281 deletions(-)

合并了服务器上最新的改动,接着执行

$ git stash pop
...... Dropped refs/stash@{0} (....)

暂存的内容恢复并在堆栈中删除它。如果你不想删除,就不要执行git stash pop命令,而执行git stash apply命令。此时你可以使用git diff -w +文件名来确认代码自动合并的情况,然后查看git status,该git addgit commitgit push就执行吧。

参考资料

  1. Git:代码冲突常见解决方法
  2. 关于git pull的问题,如何在不commit的前提下pull回来?
上一篇 下一篇

猜你喜欢

热点阅读