Deleting a git commit


These methods are dangerous. They can and will completly distroy without warning.

Removing commits with git rebase

git rebase -i can remove commits more gracefully then git reset, however, I've seen some oddities using it which may be exclusive to me. See get rebase -i for specifics on it's usage.

Removing commits with git reset

Assuming you are sitting on a single commit and would like to remove it.

$ git reset --hard HEAD~1

Note on the above. You can replace the number (1) with the number of commits you want to rewind.

If you have an uncommited change, which needs to be cleared, use the following.

$ git reset --hard HEAD

To remove multiple commits.

$ git reset --hard <sha1-commit-id>

To remove all local commits and reset to the remote origin.

$ git reset --hard origin

If you already pushed the commits you wish to delete, you will need to do a force push to get rid of it.

$ git push origin HEAD --force


The above can and will wipe out large sets of changes and do very bad things, if you do something you did not mean to. I strongly recommend doing this on a branch, or creating a backup tag before proceeding.


This post was inspried by and a good portion of these example came from there.

Published on