Wednesday, January 25, 2017

Git-fu: How to merge without actually merging

Sometimes in your life with git, you'll encounter a situation where you try to merge, for example a hotfix branch back into develop, and the merge ends up:
  1. Having a huge number of conflicts, and/or...
  2. Backing out changes in the target branch that should remain.
The reason for this usually has something to do with rebasing or cherry-picking in a way that Git can't follow, but that's not really important if you're in this situation and you need to finish up some merges quickly.

A simple solution is to have git think the merge has happened, but not actually merge the files.   This is actually very simple:

First, merge without auto-committing or fast-forwarding:

$ git merge hotfix/1.2.3 --no-commit --no-ff

This will do all the merging, but it will not create the merge commit.   You can then discard all the changes, or only some of them, and commit: 


$ git commit

Subsequent merges to the target branch will not try to re-apply any of the changes, as it thinks everything has been merged.