blog.mhartl | Michael Hartl's tech blog

2008-10-28

Using a temporary branch when doing Git merges

Filed under: Git, Insoshi — mhartl @ 15:54

Merging branches in Git is wonderfully easy compared to many other version control tools, but sometimes merging causes problems you’d rather undo. One common merge side effect is the creation of code conflicts, and sometimes a merge causes so many conflicts that you end up regretting doing the merge in the first place. In addition, for projects with many contributors (such as Insoshi), sometimes you aren’t sure if you will even want to use the contribution on the branch you’re merging in. Unfortunately, merges are very difficult to undo, so if you just do a direct merge of, say, a contributor branch into your main development branch, you’re stuck if you decide you don’t want the changes after all:

# Don't do this!
$ git checkout master
$ git merge contributor_branch

The solution is always to use a temporary branch when doing any merge whose changes you’re not sure you’ll want to keep; if the merge proves intractable due to conflicts, or you just don’t want to use the contribution, then you can simply delete the temp branch. Here’s how it works:

$ git checkout master
$ git checkout -b temp_branch
$ git merge contributor_branch

Then you can do stuff like

$ git status
$ git diff master
<resolve conflicts, polish contributed code>

If the new branch passes muster, you can then merge it in:

$ git checkout master
$ git merge temp_branch
$ git branch -D temp_branch

(Note here that I’ve deleted the temp branch in the final step, just to clean up.) If, on the other hand, you decide not to continue with the merge, you can just delete the temp branch without merging it in:

$ git checkout master
$ git branch -D temp_branch

Either way, the lesson is the same: consistently using temp branches when doing dangerous merges is a great way to avoid the agony of merge remorse.

About these ads

2 Comments

  1. If you get stuck and decide to just give up and throw the whole mess away, you can always return to the pre-merge state with

    {{{
    $ git reset –hard HEAD
    }}}

    Or, if you’ve already committed the merge that you want to throw away,

    {{{
    $ git reset –hard ORIG_HEAD
    }}}

    However, this last command can be dangerous in some cases–never throw away a commit if that commit may itself have been merged into another branch, as doing so may confuse further merges.

    Comment by Keith — 2010-01-14 @ 17:18

  2. [...] Hartl has an excellent post on how to use a temporary branch when doing a Git merge. As he explains: Merging branches in Git [...]

    Pingback by Using a Temporary Branch When Doing Git Merges | Michael Hutchinson — 2012-07-10 @ 05:00


RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

The Shocking Blue Green Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 29 other followers

%d bloggers like this: