您的位置:首页 > 运维架构

Git 分支管理-git stash 和git stash pop

2017-02-22 14:05 501 查看
合并分支,冲突是难免的,在实际协作开发中我们遇到的情况错综复杂,今天就讲两个比较重要的命令使用git stash 和git stash pop

试想一下:1.假如我们在develop分支开发,这时候突然技术经理说有个紧急修复下,这修复bug之前说了,需要从master稳定版本开一个分支,而我们develop如果没有commit,而直接切换到master,会有如下提示:

zxdeMacBook-Pro:hswallpager zs$ git checkout master
error: Your local changes to the following files would be overwritten by checkout:
app/src/main/java/Activity.java
Please, commit your changes or stash them before you can switch branches.
Aborting

zxdeMacBook-Pro:hswallpager zs$ git branch
* develop
master


根据提示,我们需要提交修改或者在切换分之前 stash 一下。而我们每次间断就要commit一次,将来git log里会有很多临时提交,太多了让人无法快速定位,而这的确不是我们想要的,那就只有stash。stash的含义就是把工作区的修改临时储藏起来,等以后再恢复使用。那我们不妨一试git stash,看看结局是什么样子的:

zxdeMacBook-Pro:hswallpager zs$ git stash
Saved working directory and index state WIP on develop: b70f2af  develop update
HEAD is now at b70f2af  develop update


先看最后一句 "HEAD is now at b70f2af develop update". 还记得上一篇的分支图吧。

*   073fc5c 合并后的修改
|\
| * b70f2af  develop update
* | 41754e3 修改
|/


因为我们上一篇master合并后develop后,并没有将develop的分支和master同步,因此develop分支的最新的提交记录就在b70f2af,也就是工作区目前是干净的。git stash  执行后,develop分支就相当于什么也没操作一样。

接着我们在执行最开始的切换到master分支,看看会怎样,还会不会提示上述信息:

zxdeMacBook-Pro:hswallpager zs$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 8 commits.
(use "git push" to publish your local commits)

zxdeMacBook-Pro:hswallpager zs$ git branch
develop
* master


怎么样,是不是正确切换到master分支了。现在我们在新建并切换分支hotfixes-01.然后可以修复紧急bug了。然后修改,提交,删除hotfixes-01即可。然后我们继续切回develop分支:

zxdeMacBook-Pro:hswallpager zs$ git checkout develop
Switched to branch 'develop'
zxdeMacBook-Pro:hswallpager zs$ git status
On branch develop

nothing to commit, working directory clean


这时候我们可以先把master分支的修改合并到develop,操作步骤以前也学过了,合并冲突等。这时候我们看会代码,stash之前的代码已经看不到了。那我们怎么继续接着上述的修改恢复现场呢。这时候用到git stash pop。

我们先看一下stash清单,执行git stash list。

zxdeMacBook-Pro:hswallpager zs$ git stash list
stash@{0}: WIP on develop: b70f2af develop update


然后我们用git stash pop 恢复现场,看一下结果:

zxdeMacBook-Pro:hswallpager zs$ git stash pop
On branch develop

Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified:   app/src/main/java/Activity.java

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (44c79bddb5c6c3848bc0de0b687cf14d4907b012)


这时候在看工作区的源代码,发现已经正确恢复现场,可以继续在以前基础上工作了。

2.现在另一种情况:你pull最新代码,但这时候你又不想重新增加commit记录,这时候先git stash,然后pull,最后在git stash pop,

这1和2两种情况在实际开发过程中会经常用到,要熟练掌握git stash的应用。

补充:在我们多次使用git stash 后,git栈里充满了很多未提交的代码,这时候用git stash list 可以讲git 栈信息打印出来,比如

git stash apply stash@{1} 就代表把指定版本号为stash@{1}的工作取出来。清空的话使用git stash clear。

git stash pop 和 git stash apply 的不同:

apply 读取暂存区的数据,通过apply后,暂存区的数据依然存在。

pop 是取出最新的一次暂存数据,pop后,暂存区就不会存在这次数据了。

总结:

git stash  #可用来暂存当前正在进行中的工作

git stash pop  #从git栈中恢复第一个。相当于git stash apply 和git stash drop

git stash list   #打印git栈中的所有信息

git stash clear  #清空git栈

git stash apply stash@{1}  #将你指定版本号为stash@{1}的工作取出

版本分支是git区分集中式版本控制的一大优势,而为了保证团队协作中顺利的开发,建议大家多用分支,至于具体分支的命名,不必拘泥死板,根据自己团队的实际情况,让分支成为我们团队开发的助推器,而不是拖后腿。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Git stash