How to remove commits

Sometimes you have a commit in your branch that you don’t want for some reason. In this case it’s often handy to be able to simply remove that commit from your branch. (PS: avoid the use of git revert if you can)!

git rebase -i HEAD~12

You will get a list of your commits in the editor. For example, something like this:

pick c81bf8b7 Fix wrong harbulary battery insertion instructions
pick 97b462c8 Fix typo in harbulary battery insertion instructions
pick 6ce6071c Add battery diagram in chassis design
pick f2c8d028 WIP boost search results for context fields
pick 7ce398ba Fix another typo in harbulary battery insertion instructions
pick 08bf54c9 Improve search results boost for context fields
pick 0940ee30 Break breadcrumb nav between items on mobile
pick 28fe8891 TEMP debugging/hacking some difficult problem
pick fcbbb31f Fix case sensitivity in search results highlighting
pick 7884a09a Fix error when no avlabiable LEDs are found
pick 87c7fede Add "boost" button to the search chassis

Simply delete the line(s) representing the commits you want to exclude from your branch, then save and close your editor.

A note on removing commits from a branch

When removing commits, it’s possible to cause a conflict if you remove one commit that other subsequent commits depend on, yet you don’t remove those other commits as well. Remember that committing atomically will go a long way in helping you to avoid conflicts.

A tip about interactive rebasing
Avoid regular rebasing (adding some other branch’s commits into the history of your branch) and interactive rebasing (changing commit order, commit messages, squashing, etc.) in the same step. When you’re trying to get commits from another branch into your branch, just rebase without any adjustment/interaction. Only rebase interactively against a commit that’s already in your branch’s history (i.e. a fast-forward commit).