How can I delete a commit in Git?
Git offers a couple of tools to delete (or undo) older commits. The first step, therefore, is to find out which tool is best for your scenario.
The Git Cheat Sheet
No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!
Restoring an old version of a project
Do you want to restore an older revision of your project - just as it was at a certain point in time?
In that case, the "git reset" command is what best suits your needs. Read more in our detailed post about how to reset to a previous revision.
Undoing only a specific commit
A different scenario is when you want to revert the effects of a certain commit - without deleting any commits that came afterwards.
This is a case for the "git revert" command. Interestingly, the command doesn't delete anything; it creates a new commit that introduces changes which revert the effects of the specified commit. Our post explains the details of how to go about reverting commits.
Delete or change specific commits
Another use case might be to delete a commit "in the middle" of your history, without resetting your whole project to a previous revision.
In that case, we'll have to bring out the big guns: Git's "Interactive Rebase" tool is what we need here. Please note that this is not only the most powerful, but also the most dangerous of the three commands listed here. This is because it allows you to change your commit history quite drastically - which is a dangerous process.
Read more about Interactive Rebase, including what to watch out for.
Tip
Deleting & Undoing Commits in Tower
In case you are using the Tower Git client, not only "reset" and "revert" are easily accessible. Tower also allows you to access advanced tools like "interactive rebase" very easily, for example to delete a commit (and if you made a mistake, you can undo it simply by hitting CMD+Z!).
Learn More
- Check out the chapter Undoing Things in our free online book
- More frequently asked questions about Git & version control