Undoing Merges

I would like to start writing more here about general Git tips, tricks and upcoming features. There has actually been a lot of cool stuff that has happened since the book was first published, and a number of interesting things that I didn't get around to covering in the book. I figure if I start blogging about the more interesting stuff, it should serve as a pretty handy guide should I ever start writing a second edition.

For the first such post, I'm going to cover a topic that was asked about at a training I did recently. The question was about a workflow where long running branches are merged occasionally, much like the Large Merging workflow that I describe in the book. They asked how to unmerge a branch, either permenantly or allowing you to merge it in later.

You can actually do this a number of ways. Let's say you have history that looks something like this:

You have a couple of topic branches that you have developed and then integrated together by a series of merges. Now you want to revert something back in the history, say 'C10' in this case.

The first way to solve the problem could be to rewind 'master' back to C3 and then merge the remaining two lines back in again. This requires that anyone you're collaborating with knows how to handle rewound heads, but if that's not an issue, this is a perfectly viable solution. This is basically how the 'pu' branch is handled in the Git project itself.

$ git checkout master
$ git reset --hard [sha_of_C3]
$ git merge jk/post-checkout
$ git merge db/push-cleanup

Once you rewind and remerge, you'll instead have a history that looks more like this:

Now you can go back and work on that newly unmerged line and merge it again at a later point, or perhaps ignore it entirely.

Reverting a Merge

However, what if you didn't find this out until later, or perhaps you or one of your collaborators have done work after this merge series? What if your history looks more like this:

Now you either have to revert one of the merges, or go back, remerge and then cherry-pick the remaining changes again (C10 and C11 in this case), which is confusing and difficult, especially if there are a lot of commits after those merges.

Well, it turns out that Git is actually pretty good at reverting an entire merge. Although you've probably only used the git revert command to revert a single commit (if you've used it at all), you can also use it to revert merge commits.

All you have to do is specify the merge commit you want to revert and the parent line you want to keep. Let's say that we want to revert the merge of the jk/post-checkout line. We can do so like this:

$ git revert -m 1 [sha_of_C9]
Finished one revert.
[master 88edd6d] Revert "Merge branch 'jk/post-checkout'"
 1 files changed, 0 insertions(+), 2 deletions(-)

That will introduce a new commit that undoes the changes introduced by merging in the branch in the first place - sort of like a reverse cherry pick of all of the commits that were unique to that branch. Pretty cool.

However, we're not done.

Reverting the Revert

Let's say now that you want to re-merge that work again. If you try to merge it again, Git will see that the commits on that branch are in the history and will assume that you are mistakenly trying to merge something you already have.

$ git merge jk/post-checkout
Already up-to-date.

Oops - it did nothing at all. Even more confusing is if you went back and committed on that branch and then tried to merge it in, it would only introduce the changes since you originally merged.

Gah. Now that's really a strange state and is likely to cause a bunch of conflicts or confusing errors. What you want to do instead is revert the revert of the merge:

$ git revert 88edd6d
Finished one revert.
[master 268e243] Revert "Revert "Merge branch 'jk/post-checkout'""
 1 files changed, 2 insertions(+), 0 deletions(-)

Cool, so now we've basically reintroduced everything that was in the branch that we had reverted out before. Now if we have more work on that branch in the meantime, we can just re-merge it.

$ git merge jk/post-checkout
Auto-merging test.txt
Merge made by recursive.
 test.txt |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

So, I hope that's helpful. This can be particularly useful if you have a merge-heavy development process. In fact, if you work mostly in topic branches before merging for integration purposes, you may want to use the git merge --no-ff option so that the first merge is not a fast forward and can be reverted out in this manner.

Until next time.

 

https://git-scm.com/blog/2010/03/02/undoing-merges.html

 

git 撤销 merging,git取消merge

$ git pull origin test
// git pull合并代码的时候,若发生冲突,会处于merging状态,检查代码,发现自己的分支低于主分支,这个时候想撤销merge

// 撤销merge
$ git reset --hard HEAD (or sha_1)

 

 

 

用git提交代码时 冲突了 

于是分支名变成如下图所示

 

在命令行输入:

 git reset --hard head 就可以了

 

 

但是在这个过程中 可能会碰到一个问题,如下图:

这个意思是在C:\Users\Admin\git\phhhh\.git目录下已经有一个index.lock文件夹存在了,只需要去这个目录下删了这个文件夹,然后再输入 git reset --hard head 就ok了

 

 http://blog.csdn.net/Android_Amelia/article/details/49612631?locationNum=11&fps=1

 

时间: 2024-09-10 02:52:07

Undoing Merges的相关文章

bug#66718 Assert from DROP TABLE concurrent with ibuf merges

http://bugs.mysql.com/bug.php?id=66718 Assert from DROP TABLE concurrent with ibuf merges A.原因: master线程发起的ibuf merge和用户线程的drop table操作可能存在竞争. 1.master线程请求文件page IO,读入内存之前,会调用buf_page_init_for_read->fil_tablespace_deleted_or_being_deleted_in_mem 如果这时

Devexpress VCL Build v2014 vol 15.2.3 发布

2016年第一个版本,继续修补. New Major Features in 15.2 What's New in VCL Products 15.2 Breaking Changes To learn about breaking changes in this version, please refer to the following page:Breaking Changes in 15.2.3 (VCL Product Line) Known Issues To learn about

index merge的数据结构和成本评估

1. 概述:index merge的数据结构 index merge的主要数据结构仍然是存放在SEL_TREE中: class SEL_TREE :public Sql_alloc { ... List<SEL_IMERGE> merges; ... }; 在merges这个list中存放了所有可能的index merge.本文将从几个案例,来看看SEL_TREE/SEL_IMERGE如何代表一个index merge访问方式.本文将不再重复介绍SEL_ARG/SEL_TREE的Range相关

git对远程分支和tag的操作

技术 Git查看.删除.重命名远程分支和tag 11/17/2012zrong7条评论69,235 次查看 本站文章除注明转载外,均为本站原创或者翻译. 本站文章欢迎各种形式的转载,但请18岁以上的转载者注明文章出处,尊重我的劳动,也尊重你的智商: 本站部分原创和翻译文章提供markdown格式源码,欢迎使用文章源码进行转载: 本文标题:Git查看.删除.重命名远程分支和tag 本文链接:http://zengrong.net/post/1746.htm Git查看.删除.重命名远程分支和tag

网页前端设计资源:前端设计资源收藏夹

文章简介:今天在邮件中收到一个由 Dimi Navrotskyy在Github上发布的前端收藏夹,里面的资源太丰富了.我在想很多同学肯定喜欢.本想直接发个链接与大家分享,但时间久了,找起来辛苦.特意copy了一份发在w3cplus上. 今天在邮件中收到一个由 Dimi Navrotskyy在Github上发布的前端收藏夹,里面的资源太丰富了.我在想很多同学肯定喜欢.本想直接发个链接与大家分享,但时间久了,找起来辛苦.特意copy了一份发在w3cplus上.而且我在后面还增加了一份我自己整理的学习

Dreamweaver MX 2004从零开始(2)

dreamweaver 用表格定位网页 使用表格可以清晰地显示列表的数据,实际上表格的作用远远不止显示数据,它在网页定位上一直起着重要的作用,尤其是对于使用非IE浏览器的网友来说,使用表格定位的网页比使用图层定位的网页更具有优势. 一.创建表格 第一步:在Dreamweaver MX 2004中运行"Insert→Table"命令,弹出属性设置窗口,其中"Rows"和"Columns"分别对应着表格的行数和列数,"Table width

Dreamweaver MX 2004从零开始

dreamweaver 随着Internet的普及,很多人已经不满足于仅仅上网冲浪,而希望深入地参与其中.现在,拥有自己的Web网站已经成为一种潮流.虽然制作一个简单的网页并不困难,但是制作出超凡脱俗的网站就不那么容易了,因此我们特意为大家准备了最新网站设计软件Dreamweaver MX 2004的系列教程,希望对大家有所帮助. 通常在一个网站中会有几十甚至几百个风格基本相似的页面,如果每次都重新设定网页结构以及相同栏目下的导航条.各类图标就显得非常麻烦,不过我们可以借助Dreamweaver

InnoDB 中文参考手册 --- 9 性能调整技巧

参考|参考手册|技巧|性能|中文 InnoDB 中文参考手册 --- 犬犬(心帆)翻译 9 性能调整技巧(Performance tuning tips)1. 如果 Unix top 或 Windows 任务管理器(Task Manager) 显示服务的 CPU 占用率小于 70%,(shows that the CPU usage percentage with your workload is less than 70 %,)你的系统瓶颈可能在磁盘读写上.或许你提交了大量的事务,或者是缓冲池

Seomoz:Google算法十年变迁史

  近日Seomoz发布了一则新的专题,有关Google算法变迁的.从2000年到2011年每一次升级都做了详细的列举,具体如下:   Google每年的算法改变高达500-600次,只不过很多改动都很小.但是每个几个月,Google都会做一次比较大的算法升级,这些升级都会直接影响到搜索的排名结果. 作为一个搜索引擎营销人员,了解这些算法的改变有助于分析网站排名以及流量的变化状况,下面我们列出了几次影响比较大的算法升级,熟悉这些算法的变动将会有助于你的SEO工作. 2000年 2000年12月