How do I undo my last commit?

Made a commit too early, included the wrong files, or just wrote a terrible commit message? It happens constantly. Git gives you several ways to undo your last commit, and the right choice depends on what you want to keep.

Keep your changes staged (soft reset)

If the code itself is fine but you want to redo the commit (maybe fix the message or add a forgotten file), a soft reset is what you need:

$ git reset --soft HEAD~1

This moves HEAD back one commit but leaves all your changes in the staging area, exactly as they were before you committed. You can make adjustments and commit again immediately.

Keep your changes but unstage them (mixed reset)

The default reset mode unstages your changes but keeps them in your working directory:

$ git reset HEAD~1

This is equivalent to git reset --mixed HEAD~1. Your files are intact, but nothing is staged. This is useful when you want to re-organise which files go into which commit.

Discard everything (hard reset)

WARNING: This permanently deletes your changes. There is no undo.

$ git reset --hard HEAD~1

This throws away the commit and all the changes that went with it. Only use this when you are absolutely certain you do not need any of the work in that commit. If you have already pushed the commit, a hard reset will also cause problems for anyone else working on the branch.

Undo a commit that was already pushed

If the commit is already on a remote branch that others are pulling from, rewriting history with git reset will cause conflicts for your collaborators. Use git revert instead:

$ git revert HEAD

This creates a new commit that reverses the changes introduced by your last commit. The original commit stays in the history, so nobody's local copy breaks.

You can learn more about the differences in our guide on git reset vs git revert.

Quick reference

Goal Command
Undo commit, keep changes staged git reset --soft HEAD~1
Undo commit, unstage changes git reset HEAD~1
Undo commit, delete changes git reset --hard HEAD~1
Undo a pushed commit safely git revert HEAD

For undoing more than one commit at a time, see how to undo recent commits. You can also read more about reverting to a previous commit or explore the full git reset reference.


DeployHQ takes the stress out of deployments by automatically deploying from your Git repository whenever you push. Try it free.