How to undo a git commit

Greg Foster
Greg Foster
Graphite software engineer

Whether you've made a mistake in your commit or simply want to modify it before pushing to a remote repository, undoing a git commit can be extremely useful. This guide covers several commands and steps you can follow ranging from completely removing a commit from your history, to undoing a commit nondestructively while retaining the changes.

To undo your most recent commit and keep all changes from that commit in your working directory, run:

git reset --soft HEAD~1

This command moves the current HEAD back one commit but keeps the changes in your staging area, allowing you to modify and recommit if needed.

If you want to completely remove the last commit and all changes associated with it:

git reset --hard HEAD~1

This will reset your working directory and index (staging area) to the state of the commit before the last.

For further reading on how to properly use the --hard flag see this guide on how to use git reset.

To undo multiple commits without discarding any of the staged changes:

git reset --soft HEAD~N

Replace N with the number of commits you wish to undo. This will not discard the changes you currently have staged.

If you want to completely clean the staging area and discard all changes from the last N commits use the --hard flag:

git reset --hard HEAD~N

If you've already pushed your commit or want to undo a specific commit safely without rewriting history, git revert is the right choice:

git revert <commit-hash>

This command creates a new commit that undoes the changes made by the commit you specify, preserving the project history.

After undoing commits, if you have untracked files you want to discard, or “clean”, run the command:

git clean -f -d

This removes untracked files and directories from your working tree, discarding them.

By default Git will refuse to delete files unless the -f flag is specified.

The -d flag instructs the clean command to recurse into untracked directories, and the following subdirectories.

While not necessary, it is highly recommended to first do a “dry run” of the git clean command by using the -n flag. By including this flag clean will not actually delete anything and instead return a list of all of the files that would be deleted when executing the command. Once you are satisfied with this output, remove the -n flag and run the clean command again to remove untracked files.

For further reading on how to undo Git commits please see the official Git documentation on git reset and git revert

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Get started

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2