user1146657 user1146657 - 12 days ago 5
Git Question

Removing merged branch commits in git

I was working with git and created an unnecessary situation by accident. This is how my git history looks like:

...a---b-------d---m---e...
\ /
\-c-----/


I would like to remove the branching. It does not matter how. I am not a git expert and tried many things without success. Ideally I would like to have:

a---b---d---m---e


I don't need a lecture on how preserving the history is important. The changes in
c
are not significant enough to be a branch.

Answer

You can use rebase for this. It will essentially reset the branch to a point you specify, then replay commits over the top again the way you tell it to. In this case you'll exclude commit C:

git rebase -i HEAD~5 # 5 is the number of commits back to rebase
# Comment out or delete the line for commit C
# Save/proceed

As you noted, you don't care about preserving the history. This process will change the commit hashes for the commits after C.

Comments