adamhl.dev

How to clean up merge commits from git commit history

1 min read


I was messing with some scripts that automatically created and then merged some PRs on some of my repos, and made the mistake of not turning off the “Allow merge commits” setting on GitHub.

allow merge commits github setting

Which gave me a commit history that looks like this:

merge commits

Oops.

Googling how to fix this brings up a lot of answers about resetting then cherry picking the commits, and other solutions that are a bit of a pain.

Turn out you can use (abuse?) git rebase to make this really easy1.

Just run:

Terminal window
git rebase <most recent commit hash before the merge commits>

Which in my case would be: git rebase 20e8c20

fixed commits

Nice.

Footnotes

  1. https://stackoverflow.com/a/60277828