git stash pop时的冲突解决
今天遇到一个问题,本来在主分支上checkout了一条新分支出来工作,可是做一半的时候突然发现后续要做的东西依赖于另一条特性分支里面的代码。特性分支其实是已经推送到远程并且提交合并请求了,但是并没有及时合入。所以跟老大沟通了一下,先把远程的特性分支合入主分支,我本地再rebase一下最新的主分支。可是因为功能才完成一半,并不属于一个完整的commit,所以我先把代码用git stash存了起来。本地rebase完执行git stash pop的时候就出现了冲突。因为以前从来没有遇到过这种情况,所以一时不知道怎么处理比较好。网上查了资料,发现上面的做法不是很好,所以自己尝试着把问题处理了。

干讲命令的话估计不好理解,下面我通过一个例子来演示一下整个冲突出现和解决的过程。由于只是展示怎么解决冲突,不涉及具体生产时的工作流程,所以就直接在本地进行了。本文为了尽可能地把问题讲清楚,手动复现了冲突,所以冗余的内容较多,可能对于Git老手来说显得有点啰嗦,如果想直接看结论,请直接跳转到第六步。

第一步,我们先做好准备工作,初始化一个版本仓库,新建一个文件test并且提交,作为master分支上的根commit:

$ git init

$ touch test

$ git add test

$ git commit -m "init test"
[master (root-commit) c1f4cda] init test
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test

$ git log
commit c1f4cdac5e3ede10a995c42b9c1d5b490b0d09a2 (HEAD -> master)
Author: ganziqim <ganziqim@live.com>
Date:   Mon Nov 13 14:14:01 2017 +0800

init test

第二步,切换到另一条分支上工作,新建另一个文件anothertest并提交commit,代表在新分支上的某个工作阶段的成果:

$ git checkout -b another
Switched to a new branch 'another'

$ touch anothertest

$ git add anothertest

$ git commit -m "init anothertest"
[another 66966d5] init anothertest
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 anothertest

$ git log
commit 66966d50a3128771bf70c06ab2b6bd05ef748337 (HEAD -> another)
Author: ganziqim <ganziqim@live.com>
Date:   Mon Nov 13 14:14:56 2017 +0800

init anothertest

commit c1f4cdac5e3ede10a995c42b9c1d5b490b0d09a2 (master)
Author: ganziqim <ganziqim@live.com>
Date:   Mon Nov 13 14:14:01 2017 +0800

init test

第三步,在another分支上修改test和anothertest文件的内容,使用git stash进行暂存,代表工作一半的成果,无法作为一个完整的commit进行提交:

$ vim test

$ vim anothertest

$ git stash
Saved working directory and index state WIP on another: 66966d5 init anothertest

$ git stash list
stash@{0}: WIP on another: 66966d5 init anothertest

第四步,回到master分支,修改test文件并提交commit,模拟master分支出现了another分支需要使用的代码。这个时候分支树出现了分叉,master分支和another分支各自往前进行了一个commit:

$ git checkout master
Switched to branch 'master'

$ vim test

$ git add test

$ git commit -m "changes on branch master"
[master 9bc290f] changes on branch master
 1 file changed, 1 insertion(+)

$ git log
commit 9bc290f5b6cea5757e01af64ea8c2591c9debdf9 (HEAD -> master)
Author: ganziqim <ganziqim@live.com>
Date:   Mon Nov 13 14:18:11 2017 +0800

changes on branch master

commit c1f4cdac5e3ede10a995c42b9c1d5b490b0d09a2
Author: ganziqim <ganziqim@live.com>
Date:   Mon Nov 13 14:14:01 2017 +0800

init test

第五步,再回到another分支,使用git rebase进行变基,模拟another分支取得master分支上的最新代码,这时分支树又变成了一条直线:

$ git checkout another
Switched to branch 'another'

$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: init anothertest

$ git log
commit 0687e2a2f0d243a725de6093d9f42de5ab02acfd (HEAD -> another)
Author: ganziqim <ganziqim@live.com>
Date:   Mon Nov 13 14:14:56 2017 +0800

init anothertest

commit 9bc290f5b6cea5757e01af64ea8c2591c9debdf9 (master)
Author: ganziqim <ganziqim@live.com>
Date:   Mon Nov 13 14:18:11 2017 +0800

changes on branch master

commit c1f4cdac5e3ede10a995c42b9c1d5b490b0d09a2
Author: ganziqim <ganziqim@live.com>
Date:   Mon Nov 13 14:14:01 2017 +0800

init test

第六步,事情进展到目前来看,都非常顺利,特性分支成功地取得了主分支上需要用到的代码。但是当我们执行git stash pop想取出之前工作一半的成果之后,却出现了冲突,其原因是主分支上的最新代码和stash暂存的代码对同一个文件都进行了修改。

$ git stash pop
Auto-merging test
CONFLICT (content): Merge conflict in test

$ git status
On branch another
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

modified:   anothertest

Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add <file>..." to mark resolution)

both modified:   test

用git status查看状态的时候,可以看到Git是有标出冲突的文件的,是不是很熟悉呢?对,这个时候先按照解决普通的pull conflict的方式修改文件,然后执行git add。如果完全接受主分支的修改,那么再次查看git status的时候这个文件应该不会再出现在状态里了,但这里我们模拟的是冲突两边的修改我们都想保留的情况。这时候代表你个人已经接受当前的冲突解决了,Git会把修改完的结果包括其它没有出现冲突的文件放入暂存区。

$ vim test

$ git add test

$ git status
On branch another
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

modified:   anothertest
        modified:   test

其实事情进行到上面那一步已经算作解决了,可是有时候你并不想把这些文件中的某一个作为下个commit的内容提交到远程,所以此时再执行一次git reset HEAD,就恢复git stash pop后该有的状态了。

$ git reset HEAD
Unstaged changes after reset:
M       anothertest
M       test

$ git status
On branch another
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:   anothertest
        modified:   test

no changes added to commit (use "git add" and/or "git commit -a")

需要注意的是,冲突解决之后,Git并不会删除之前的stash记录,可以使用git stash drop将没用的记录删除掉。

$ git stash list
stash@{0}: WIP on another: 66966d5 init anothertest

$ git stash drop stash@{0}
Dropped stash@{0} (113018d1c2be77ffae51afc15944d8620619ef7d)

$ git stash list

总结
Git属于越用越顺手的东西,并且有很多实用的骚操作技巧,所以建议大家平时多多练习,没事多捣鼓捣鼓,熟能生巧嘛!而且我是推荐直接使用命令行的,GUI工具虽然方便,但有时候处理问题的速度跟不上思维,而使用命令行更顺手,尤其是专注工作,脑子快速运转的时候。相信你会爱上那种感觉的!
————————————————
版权声明:本文为CSDN博主「GanZiQim」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/jy692405180/article/details/78520251

另外的方法参考这篇文章,也是我经常用的方法,比较笨~

https://blog.csdn.net/u011217649/article/details/73744666

执行git stash pop时的冲突解决相关推荐

  1. 撤消git stash pop导致合并冲突

    本文翻译自:Undo git stash pop that results in merge conflict I began making changes to my codebase, not r ...

  2. STS 使用git 更新代码时,冲突解决

    好记性不如烂笔头. 问题:在项目更新代码时,发现本地已经修改的文件,在git上又有新版本,导致的代码冲突,无法更新与提交. 解决:1.Team--stashes(隐藏)--stash changes- ...

  3. [Git] git pull冲突和git stash pop冲突解决

    一.git pull 冲突 代码仓有人提交了新代码,而我本地也修改了代码,想要pull一下,却发现: error: Your local changes to the following files ...

  4. Git之解决git stash pop多次产生的文件冲突问题

    1.问题 我们用git命令一般拉取线上代码的时候,本地修改了,我们一般先git stash下,接下来git pull, 然后git stash pop下,但是我新增了文件,没有添加到本地git(也就是 ...

  5. git stash pop冲突_这有一份 git 日常使用清单,你需要吗?

    点击上方"前端教程",选择"星标" 每天前端开发干货第一时间送达! 作者:echozh juejin.im/post/5d5b4c6951882569eb570 ...

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

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 合并分支,冲突是难免的,在实际协作开发中我们遇到的情况错综复杂,今天就讲两个比较重要的命令使用git ...

  7. git stash和git stash pop

    2019独角兽企业重金招聘Python工程师标准>>> git stash 可用来暂存当前正在进行的工作, 比如想pull 最新代码, 又不想加新commit, 或者另外一种情况,为 ...

  8. 执行git commit命令时提示Please tell me who you are.

    问题:执行git commit命令时提示"Please tell me who you are." 猜测是太久没使用了,它需要验证是谁在用, 解决方案 在git界面输入以下指令. ...

  9. 如何解决Git合并merge时的冲突(conflict)

    1. Git Extensions 图形界面如何切换分支 2. Git merge时的冲突如何解决 通过图形化git工具可以手动查看冲突并选择你认为正确的合并后的最终结果,具体步骤如下: 点击保存后冲 ...

最新文章

  1. 那个14岁上大学、17岁读博、24岁成教授的天才神童,如今怎样了?
  2. vss2005与vs2005绑定问题解决
  3. 用 Parser Combinator 解析 Cirru 的缩进语法
  4. hdu1395 2^x mod n = 1
  5. 7 项目人力资源管理
  6. Spring(一)——用Spring IOC容器创建对象
  7. 安装ROS环境时的常见问题及解决办法
  8. java成绩查询_JavaWeb项目第三次总结_成绩查询的实现
  9. VS2015配置环境支持opencv3库(网络方法总结)
  10. 2.3基本算法之递归变递推 1188 菲波那契数列(2)
  11. linux mint 安装内核,如何在Ubuntu, Linux Mint中安装Linux Kernel 4.18
  12. c++11新特性的使用---可变模板参数、type_traits、function综合使用
  13. php-5.6.26源代码 - hash存储结构 - hash算法
  14. 【漏洞复现】PHPmyadmin 4.8.1后台Getshell新姿势
  15. 进程被kill原因_Linux内核系列 简析进程生命周期:从生到死的这一生(一)
  16. LINUX的VirtualBox安装Windows7
  17. 【转】网站ICP备案和公安备案流程
  18. MyEclipse 10 破解
  19. 比较PAFF和MBAFF
  20. Vue项目:IE11中地址栏直接改变路由页面不跳转bug

热门文章

  1. 15类图神经网络的应用场景总结
  2. python中同级目录下不同py文件之间的调用失败问题
  3. Ubuntu16.04下载截屏录屏软件
  4. matlab保存数据用什么指令_MATLAB文件操作及保存文件save load fopen | 学步园
  5. 一种快速生成边界交通场景数据的新方法
  6. 观点丨Fortinet谈ChatGPT火爆引发的网络安全行业剧变
  7. 【莹伙丛】手把手教你:Gradle 安装及配置
  8. 微信公众号最佳实践 ( 3.2) 被动回复用户消息
  9. Autosar E2E功能安全算法实现
  10. iOS开发支付 — 内购(IAP)