title: How to fix the history in a wrong merge
body: <p>I have a problem in Git that I am unable to fix. I have searched for similar cases but I was unable to find a good solution for my situation.</p>
  
  <p>I basically have 2 branches, master and bugfix. Bugfix is derived from master at a point when we've done a release. Every time we fix something in bugfix and do a bugfix release we need to merge that in master. Master contains the new features (and of course should never be merged into bugfix). It so happens that someone did merge master with bugfix by mistake. </p>
  
  <pre><code>This is what happened:
  
   master: x1---x2
                  \
   bugfix: y1--y2--M
  
  where M is the merge between bugfix and master
  </code></pre>
  
  <p>A solution for this was to revert the merge which I did with "git revert -m 1 M" (M is here the SHA from the merge) as described <a href="http://opensource.apple.com/source/Git/Git-26/src/git-htmldocs/howto/revert-a-faulty-merge.txt" rel="nofollow">here</a>. I did a revert because we've already had other changes in the bugfix branch that we did not want to lose and this worked fine for the bugfix branch.</p>
  
  <pre><code>Current state:
  
   master: x1--x2------------x---x
  
   bugfix: y1--x1--y2--x2--M--y--y--R1
  
  where R1 is the revert of M
  </code></pre>
  
  <p>The problem appears later on when we need to merge the bugfix branch in master. As described in the link I've posted, the revert fixes the code changes but not the history, this means that when I merge bugfix in master the revert is going to take effect and remove the changes from the master (x1 and x2). What I can do is a revert of the revert in the bugfix before I merge it in master. </p>
  
  <pre><code>Solution:
  
   master: x1--x2------------x---x--------M
                                         /
   bugfix: y1--x1--y2--x2--M--y--y--R1--R2
  
  where R2 is the revert of R1
  </code></pre>
  
  <p>This works but is not a final solution because every time we do a merge we would need to do this "revert of the revert". I have been looking for a permanent solution to this for a few days now and it seems that the only option would be to erase the master commits (x1 and x2) from the bugfix branch's history. But x1 and x2 are used here just as an example, in reality we have many more commits and dissecting the history of the bugfix branch is very difficult and error prone. I am certain that Git can help me do this but I don't know how.</p>
  
  <p>Any ideas would be very helpful.</p>