Few commits ago I accidentally did a nonlinear merge in my master branch. I have a habit of always trying to keep a linear history, so now I would like to restore the linearity.
I have made a dummy repo, which simulates the real situation I'm having for the purposes of making this more simple. Here's a GitHub link to it: https://github.com/ruohola/merge-question
Here's the output of git log --oneline --graph --date-order
:
* 88a4b7e (HEAD -> master, origin/master, origin/HEAD) 11
* 5aae63c 10
* 5506f33 Merge branch 'other'
|\
| * b9c56c9 9
* | 3c72a2a 8
| * 8d2c1ea 7
| * 35f124b 6
* | 7ca5bc1 5
* | b9e9776 4
| * fd83f02 3
|/
* 4fa8b2e 2
* cbdcf50 1
Same graph in Sourcetree:
And here is a mspaint visualization of how I would like to get my master to look like:
(it should essentially be like I would've rebased before the merge)
I know that this might not be the best practice and I am familiar with the consequences of rewriting history (no one else is working on this branch though), but would still want to be able to do this. How can this be accomplished?
One approach would be to use rebase.
Regardless of the approach you choose, you will have to rewrite the history of your repository. You have to accept that, otherwise you will have to accept your current history.
Let's summarize the different sections of your history:
To solve this, I would do the following:
Here's diagrams of the process, step by step (commands follows):
Status now:
New branch for 9:
Rebase on top of 8, this creates 3', 6', 7', 9' (the
'
means "copy of commit, same contents, new hash")Create a new branch for 11 (I don't like to mess with master)
Rebase this branch (10 and 11) on top of TEMP1:
Verify that TEMP2 is identical to current master, nothing lost, nothing added, etc.
Then hard-reset master to TEMP2:
I would then delete branches TEMP1 and TEMP2.
Note that commit 3, 6, 7, 9, M, 10 and 11 still exists in the repository but they're not directly available because nothing refers to them. They're thus eligible for garbage collection and in reality the actual history of your repository now looks like this:
The commands to perform these operations are:
(step 0: Make a complete copy of your local folder, complete with working folder and .git repository, then, if you can, do the following commands in that copy, if you screw up, delete the copy and start over, don't jump without a safety net)
git checkout <HASH-OF-9>
git checkout -b TEMP1
(yes, you can do this and the previous command in one command withgit checkout -b TEMP1 <HASH-OF-9>
)git rebase -i --onto <HASH-OF-8> <HASH-OF-2> TEMP1
git checkout -b TEMP2 <HASH-OF-11>
git rebase --onto TEMP1 <HASH-OF-MERGE> TEMP2
git checkout master
git reset --hard TEMP2
Lastly, cleanup:
Only force-push when you know everything is OK
Perhaps the simplest way this can be done is to "abuse" the default behavior of
git rebase
. That is, without explicitly passing--preserve-merges
togit rebase
, it will actually remove all merge commits from the history. This allows us to get the desired result extremely easily:Before:
Running the command:
After:
After this, just a simple
git push -f origin master
and the remote's history is back to linear.I think it's not that hard, just keep in mind it requires rewriting history of master:
And now you could force-push the branch if you already have the old master in another remote