Commits

Git undo last commit: reset local commits, revert pushed

Git undo last commit: git reset keeps or drops your local changes, git revert undoes a commit already pushed. Commands, effects and how to recover.

On this page

How do you undo the last commit in Git?

There is no single git undo last commit command. Before you push, use git reset to take the commit off your branch. After you push, use git revert to cancel it with a new commit.

Pick the line that matches what you want to undo:

  • Undo the commit but keep its changes staged: git reset --soft HEAD~1
  • Drop the commit and its changes: git reset --hard HEAD~1
  • Cancel a commit you already pushed: git revert HEAD

HEAD is the commit you are on, and HEAD~1 is the previous commit. The outputs below come from Git 2.23, in a throwaway Git repository whose last commit is Add archived filter.

Undo the last commit with git reset: keep or drop your changes

git reset moves your current branch back to the previous commit. The mode you pass decides what happens to the changes of the removed commit. To reset the two most recent commits instead, use HEAD~2.

  • --soft leaves your files and the index (the staging area) unchanged. The commit is gone, but the changes you made stay staged, ready for a new commit. This is what people mean by “git uncommit”.
  • --mixed is the default, so git reset HEAD~1 does the same with nothing staged. Your working directory still holds the changes, and you git add what you want again.
  • --hard overwrites all files with the version of the commit you reset to, and may overwrite untracked files. Uncommitted edits in tracked files are lost too, not only the changes of the removed commit.

After the soft reset, the change is back in the staging area:

On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   search.ts

Before a hard reset, run git log --oneline -2 to see which commit you are about to drop. If only the commit message is wrong, or you forgot a file, you do not need to undo anything. Amend the last commit with git commit --amend instead.

Undo a pushed git commit on a shared branch with git revert

Once a commit is in the remote repository, other people may already have it. Pro Git says to amend only commits that are still local and have not been pushed. The same logic holds for reset: using git reset on a pushed commit, then force pushing, rewrites the remote branch under your collaborators.

git revert HEAD records a new commit that reverses the effect of the last one. The original commit stays in the history, followed by the revert commit. The Git documentation says revert requires a clean working directory, so commit or stash your edits first:

git revert HEAD
git push

A plain git push then sends it, on GitHub or any other remote. To revert a specific commit further back, pass its commit hash, found with git log --oneline, to git revert <hash>.

Recover an undone commit with git reflog

Reset the wrong commit? The reflog records when the tips of branches and other references were updated in your local repository. git reflog lists the previous positions of HEAD:

92bbb3c HEAD@{0}: reset: moving to HEAD~1
d455e26 HEAD@{1}: commit: Add archived filter

d455e26 is the commit you dropped. git branch rescue d455e26 saves it on a new branch without touching yours. To go back to it on your current branch, use git reset --hard d455e26, after committing or stashing any new work.

The reflog only knows commits. Pro Git warns that anything you lose that was never committed is likely never to be seen again. That is the real risk of --hard.

Entries do not last forever either. The git reflog documentation sets a default of 90 days, and 30 days for entries no longer reachable from the branch tip. A commit removed by a reset is in that second case, so do not count on more than 30 days.

In GitMini, a commit you make in the app ends with an Undo toast: it takes the commit back and leaves its changes staged, as git reset --soft does. The reflog panel, revert and branch reset cover the other cases.

Sources