Skip to main content
Download

Undo a Git commit: reset, revert and restore

Committed too quickly? Pushed a bug? Don't panic. Git offers several ways to undo. This page explains which one to use depending on your situation.

GitQuest
Demo

By Anaïs Sparesotto, web developer and head of education · Updated

Which command should you use?

The right way to undo depends on one question: where is the change now? Still in your files, in a local commit, or in a commit you already pushed? Answer that, and the command follows.

Not committed yet

git restore

You modified or staged files but haven't committed. You want the previous version back.

git restore <file>
Local commit (not pushed)

git reset or --amend

You committed but haven't pushed. You can rewrite your own history freely.

git reset --soft HEAD~1
Already pushed

git revert

Others may have the commit. Undo it with a new commit instead of rewriting history.

git revert <hash>

Before you undo: the three places a change can live

Git keeps your work in three zones. The working tree is the files you edit. The staging area (or index) holds what you selected with git add. The history is the chain of commits, and HEAD points to the one you are on.

Every undo command moves things between these zones. git restore works on files. git reset moves the branch back and decides what happens to the staging area and the files. git revert adds a new commit and leaves the past alone. In the examples, HEAD~1 means "one commit before HEAD", and HEAD~3 means three commits back.

A good reflex before any undo: run git status and git log --oneline -5 to see exactly where you are.

Step 0: nothing committed

Undo changes that are not committed: git restore

If you haven't committed yet, you don't need to undo a commit at all. git restore <file> throws away the edits in your working tree and puts the file back to its staged version, or to the last commit if nothing was staged. Those edits are not saved anywhere, so check the file first.

git restore --staged <file> does the opposite job: it takes the file out of the staging area and keeps your edits in the file. Use it when you ran git add . a bit too fast.

Want to keep the edits for later instead of deleting them? Put them aside with git stash. More options in the git restore guide.

Local commit

Undo the last local commit: git reset --soft, --mixed or --hard

git reset HEAD~1 moves your branch back one commit, as if the last commit never happened. The three modes only differ in what they do with the content of that commit. All three examples start from the same history:

--soft: keep everything staged

The commit is removed, but its changes are still in the staging area, ready to be committed again. This is the right choice to redo a commit differently, for example to split it or to merge it with the next one. Git prints nothing, which is normal.

--mixed: keep the changes, unstaged

This is the default mode when you type git reset HEAD~1. The commit and the staging are undone, your files stay as they are. You pick again what to git add.

--hard: throw everything away

The commit, the staging area and the changes in tracked files are all discarded. Your files go back to the state of HEAD~1. The commit itself can still be found with reflog (see below), but uncommitted edits wiped by --hard are gone for good. Untracked files are not touched.

Several commits: change the number. git reset --soft HEAD~3 undoes the last three commits and leaves all their changes staged, a simple way to squash them into one.

Changed your mind? Right after a reset, Git keeps the previous position in ORIG_HEAD, so git reset --hard ORIG_HEAD puts you back. Full details in the git reset guide.

Local commit

Fix the last commit instead of undoing it: git commit --amend

Often you don't want to undo the commit, just fix it: a typo in the message, a forgotten file, a debug line left behind. Stage the fix, then run git commit --amend. Git replaces the last commit with a new one that includes the staged changes. --no-edit keeps the message, -m sets a new one.

The amended commit gets a new hash. It is a rewrite of history, so the same rule as reset applies: amend only commits you haven't pushed. See the git commit guide for the other options.

Already pushed

Undo a commit that is already pushed: git revert

Once a commit is on the remote, teammates may have pulled it. If you reset it locally, your branch and theirs no longer agree, and a normal git push is rejected. git revert <hash> solves this cleanly: it creates a new commit that applies the exact inverse of the old one. History only moves forward, so nobody has to resync.

Several commits: git revert --no-commit HEAD~3..HEAD reverts the last three commits in your working tree, then a single git commit records them as one revert. Without --no-commit, you get one revert commit per commit.

A merge commit: a merge has two parents, so Git needs to know which side to keep. -m 1 keeps the first parent, usually the branch you merged into.

Must you really remove a pushed commit, for example a leaked password? Then reset locally and push with git push --force-with-lease, after warning your team. Unlike --force, it refuses to overwrite commits you haven't seen. A secret that was pushed should also be revoked: removing the commit does not erase copies others already fetched. More in the git revert guide.

Safety net

Recover a commit after a bad undo: git reflog

A commit removed by git reset or --amend is not deleted right away. Git records every move of HEAD in the reflog: commits, resets, checkouts, rebases. Find the line just before your mistake, then go back to it with git reset --hard HEAD@{n}, or create a branch on that hash to look at it calmly.

Two limits to know: the reflog is local to your machine (it is never pushed), and old entries expire, after 90 days by default, or 30 days for commits no longer reachable from a branch. It also cannot bring back edits that were never committed. The git reflog guide covers it step by step.

Decision table: undo a commit in every situation

Find your situation in the first column. "Rewrites history" means you should only do it on commits you haven't pushed.

Which Git command to use to undo a change, by situation
SituationCommandRewrites historyYour changes
Edited a file, not staged, want the previous version backgit restore <file>No, safe if pushedLost (the unstaged edits)
Staged a file by mistakegit restore --staged <file>No, safe if pushedKept in the file
Last commit has a typo or is missing a file (not pushed)git commit --amendYes, local onlyKept, added to the new commit
Undo the last commit, keep changes stagedgit reset --soft HEAD~1Yes, local onlyKept, staged
Undo the last commit, keep changes unstagedgit reset HEAD~1Yes, local onlyKept, unstaged
Throw away the last commit and its changesgit reset --hard HEAD~1Yes, local onlyLost (commit recoverable via reflog)
Undo a commit that is already pushedgit revert <hash>No, safe if pushedUndone by a new commit
Undo a merge that is already pushedgit revert -m 1 <hash>No, safe if pushedUndone by a new commit
Get back a commit after a bad resetgit reflog, then git reset --hard HEAD@{n}Yes, local onlyCommit restored

Common mistakes when undoing a commit

Using --hard when you meant --soft

--hard also wipes the uncommitted edits in your files. If you only want to redo the commit, --soft or the default mode is enough. When in doubt, run git stash first.

Resetting or amending a pushed commit

The next push is rejected, and forcing it rewrites your teammates' history. On a shared branch, use git revert.

Expecting git restore to undo a commit

git restore only acts on files. The commit stays in the history. To remove the commit itself, use reset or revert.

Reverting a merge without -m

Git stops with "is a merge but no -m option was given". Add -m 1 to keep the branch you merged into.

Counting HEAD~n on the wrong branch

HEAD~2 is relative to the branch you are on. Check with git log --oneline before you reset, not after.

Panicking after a mistake

A committed change is very hard to lose in Git. Before trying random commands, open git reflog: your commit is almost always there.

To undo other things than commits (a file, a merge in progress, untracked files), read the full guide how to undo changes in Git.

A

GitQuest is created by Anaïs (nouvelle fenêtre), web developer and head of education, specializing in tech training and digital accessibility.

Questions about undoing commits

Learn to undo without panicking

GitQuest trains you to master reset, revert and restore in realistic situations.