Published on

Removing that Pesky Git Commit

Authors
  • avatar
    Name
    Linell Bonnette
    Twitter

So you're merrily programming and finally finish up that cool new thing. You fix everything up and push to GitHub for you next pull request. Boo. You based your feature branch off of a branch that doesn't match where you need it to go.

As in:

  • staging
    • A-B-C-D
  • bad-branch
    • A-B-C-D-E
  • good-branch
    • A-B-C-D-E-F

So you want A, B, C, D, and F. E shouldn't be there. How can you easily fix it?

git rebase -i HEAD~N, where N is the number of commits you need to go back. In our case, it would be git rebase -i HEAD~3. This will bring up a prompt that looks something like this:

don't judge my commits

All you have to do is delete the line corresponding to the commit you want to remove. Completely remove it. Then save and quit from the prompt and BOOM done. It's really that simple. Your commits will now look like this:

  • A-B-C-D-F