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-29 03:21:51

Undoing Merges的相关文章

git push 出现 you are not allowed to upload merges 错误提示

解决方法:敲git rebase 然后按提示执行就行了,如果rebase 发生冲突的话,则修改,修改完后git add -u filename,然后git rebase --continue ,直到没有冲突为止.不需要commit .最后再上code(参考链接 参考链接2)出现问题的原因:在本地分支ahead 远程分支commit 不止一个的 时候,即是出现了分叉,在这种情况下使用了git pull 更新代码之后去git push 就会产生如此情况解决原理: 使用rebase,”放弃“本地补丁,

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 abou

转-git feature branch workflow

https://www.atlassian.com/git/tutorials/comparing-workflows/feature-branch-workflow Getting Started Setting up a repository git init git clone git config Saving changes git add git commit Inspecting a repository git status git log Viewing old commits

UNREAL ENGINE 4.12 正式发布!下载地址

UNREAL ENGINE 4.12 正式发布! 下载地址:https://www.unrealengine.com/ Alexander Paschall 在 June 1, 2016 |功能新闻社区 Share on Facebook Share on Twitter Share on Google+ Share on LinkedIn 此版本内含虚幻引擎 4 的数百个更新,以及 GitHub 虚幻引擎开发者社区提交的 106 项改良!特此对虚幻引擎 4.12 版本的贡献者们表达诚挚谢意:

Git手册

GitUserManualChinese - Robin Wiki GitUserManualChinese Git 用户手册(1.5.3 及后续版本适用) 翻译: 罗峥嵘 (Robin Steven) < [email protected] > 英文版本: http://www.kernel.org/pub/software/scm/git/docs/user-manual.html Contents Preface 前言 Chapter 1. Repositories and Branch

mysql之show engine innodb status解读(转)

add by zhj: 我第一次知道这个命令是线上服务出了问题,然后同事用这个命令去查看死锁.但用这个命令看死锁有一定的局限性,它只能看到最后一次死锁, 而且只能看到死锁环中的两个事务所执行的最后一条语句(即被死锁卡住的那条语句),看不到整个死锁环,也看到不整个事务的语句.但是即使这亲,对我 们来说也非常有用,因为一般来说,数据库同时存在多个死锁环的可能性比较小,而且有了死锁环中的事务的最后一条语句,我们找到整个死锁环不是太难. "show engine innodb status"这

误删除innodb ibdata数据文件-之恢复

今天在群里看到有人说不熟悉innodb把ibdata(数据文件)和ib_logfile(事务日志)文件误删除了.不知道怎么解决.当时我也不知道怎么办.后来查阅相关资料.终找到解决方法.其实恢复也挺简单的.我们不知道的时候就觉得难了.谁说不是这样呢? 下面我们就来模拟生产环境下,人为删除数据文件和重做日志文件.然后详细说明恢复步骤. 1.用sysbench模拟数据的写入,如下所示: [[email protected] ~]# sysbench --test=oltp --oltp-table-s

Learning docker--Chapter2--Handling Docker Containers

Content: (1)    Clarifying the Docker terms (2)    Working with the Docker images and containers (3)    The Docker Hub Registry (4)    Searching the Docker images (5)    Working with an interactive container (6)    Tracking the changes inside the con

Retrofit 入门学习

Retrofit 入门学习官方RetrofitAPI 官方的一个例子 public interface GitHubService { @GET("users/{user}/repos") Call<List<Repo>> listRepos(@Path("user") String user); } 这些注解都有一个参数 value,用来配置其路径,比如示例中的 users/{user}/repos, 我们还注意到在构造 Retrofit