The Problem
Sometimes after merging a branch, you realise that you’d like to create a nice linear history, as you would if you had rebased that branch with git rebase, instead of merging it.
Depending on your team’s outlook, this is either a sensible ‘clean repository’ step, or a significant step to obsessive compulsive disorder. The diagram shows the situation, whereby you want to move the commits of my-branch onto master in one seamless line.
It’s important to recognise that this will create new commit hashes, so you should be wary of doing this on a branch that has been published. If you do, then other people’s history will change, and that may cause havoc for them.
The Solution
This checks out the commit and allows you to work on it, and its ancestors:
This effectively moves your commits on my-branch, up to d2bb4ba, onto the tip of a new base, 8029ced:
This reapplies all the subsequent commits that were on master, from e7cdf7b onwards:
git cherry-pick e7cdf7b..master
Important: At this point, carefully check the history that you’ve created!
Warning: This overwrites the master branch entirely:
And now see the results of your effort: