Undoing Changes: restore, reset, revert and Detached HEAD
Pick the right undo command: discard or unstage edits with git restore, bring back old or deleted files, move a branch back with reset --soft, --mixed, --hard and --keep, undo pushed commits safely with git revert, and look at or build on old commits with a detached HEAD.
kvi5hnuCode Sprout team member8 min readOn this page
Git has several "undo" commands because there are several things you might want to undo: an edit you haven't staged, something you staged, a commit you haven't pushed, or a commit everyone already has. Picking the right one is mostly about where the mistake is and whether others have it. This article covers each case, from the safest to the most destructive.
| You want to undo... | Command |
|---|---|
| unstaged edits to a file | git restore <file> |
| staging (keep the edits) | git restore --staged <file> |
| a file's content, back to an older version | git restore --source=<commit> <file> |
| the last local commits, keeping the changes | git reset --soft HEAD~N or git reset HEAD~N |
| the last local commits, and their changes | git reset --hard HEAD~N |
| a commit that was already pushed | git revert <commit> |
| a merge | article 25 |
| something you did with reset, rebase or a deleted branch | the reflog, article 26 |
Undoing edits with git restore
git restore works on files. It never moves branches:
git init -q trail-guide && cd trail-guide
printf 'Lake loop: 5 km\nRidge loop: 8 km\n' > trails.txt; git add .; git commit -q -m "Add trails"
echo "Swamp loop: 2 km" >> trails.txt # an edit we regret
git restore trails.txt # discard it: back to the staged version
cat trails.txt
echo "Forest loop: 6 km" >> trails.txt
git add trails.txt # staged by mistake
git restore --staged trails.txt # unstage, keep the edit
git status --short
Lake loop: 5 km
Ridge loop: 8 km
M trails.txt
git restore <file>overwrites the working-tree file with the staged version, which is the last commit unless you staged something since. The discarded edits are gone for good, because Git never saw them.git restore --staged <file>unstages: it resets the staged version to the last commit, and leaves your working file alone. The older spelling isgit reset <file>.git restore --staged --worktree <file>(-SW) does both, returning the file completely to the last commit.git restore -p <file>asks about each change separately, so you can discard some hunks and keep others.git restore .discards every unstaged edit under the current folder. It doesn't delete untracked files, which is the job ofgit clean(article 28).
Before Git 2.23 these were done with git checkout -- <file> and git reset HEAD <file>, which still work and appear in many older guides.
Old versions and deleted files
--source restores a file as it was in any commit:
git init -q trail-guide && cd trail-guide
echo "Lake loop: 5 km" > lake.txt; echo "Ridge loop: 8 km" > ridge.txt; git add .; git commit -q -m "Add pages"
echo "Lake loop: 5.4 km (new bridge)" > lake.txt; git commit -q -am "Update lake distance"
git rm -q ridge.txt; git commit -q -m "Remove ridge page"
echo "Forest loop: 6 km" > forest.txt; git add .; git commit -q -m "Add forest page"
echo "=== lake.txt as it was in the first commit"
git restore --source=HEAD~3 lake.txt; cat lake.txt
git restore lake.txt # changed our mind
echo "=== which commit deleted ridge.txt?"
git log --oneline --diff-filter=D -- ridge.txt
echo "=== restore it from the commit before that one"
deleter=$(git log --format=%h -1 --diff-filter=D -- ridge.txt)
git restore --source="$deleter~1" ridge.txt
cat ridge.txt; git status --short
=== lake.txt as it was in the first commit
Lake loop: 5 km
=== which commit deleted ridge.txt?
2313a6e Remove ridge page
=== restore it from the commit before that one
Ridge loop: 8 km
?? ridge.txt
The restored files are ordinary changes in your working tree: review them, then commit. To restore into the staging area as well, add --staged --worktree. The older form git checkout <commit> -- <file> does that in one step.
--diff-filter=D shows only commits that deleted the given file. The file doesn't exist in that commit, so restore from its parent, <commit>~1.
Moving a branch back with git reset
git reset <commit> moves the current branch to another commit. Commits after that point drop off the branch. What happens to their changes depends on the mode:

git init -q trail-guide && cd trail-guide
echo "Lake loop: 5 km" > trails.txt; git add .; git commit -q -m "Add lake loop"
echo "Ridge loop: 8 km" >> trails.txt; git commit -q -am "Add ridge loop"
echo "Forest loop: 6 km" >> trails.txt; git commit -q -am "Add forest loop"
git branch two-commits # bookmark, to return here
for mode in soft mixed hard; do
git reset -q --hard two-commits
git reset -q --$mode HEAD~2
echo "=== --$mode: branch at '$(git log --format=%s -1)', status:"
git status --short
done
=== --soft: branch at 'Add lake loop', status:
M trails.txt
=== --mixed: branch at 'Add lake loop', status:
M trails.txt
=== --hard: branch at 'Add lake loop', status:
--softkeeps everything staged. Use it to redo commits: for example, squash the last three into one withgit reset --soft HEAD~3, thengit commit.--mixed, the default, keeps the changes as unstaged edits. Use it to reorganize: re-stage and commit in different groups.--harddiscards the commits' changes and any uncommitted edits. Use it to throw work away, or to match another commit exactly, as ingit reset --hard origin/main(article 19).
The commits themselves aren't deleted: they're just no longer on the branch. ORIG_HEAD points to where the branch was before the reset, so git reset --hard ORIG_HEAD undoes a reset immediately, and the reflog keeps the old positions for longer (article 26). What --hard can't bring back is uncommitted work it overwrote.
Safer variants: --keep and --merge
git reset --keep <commit> moves the branch like --hard, but refuses if it would overwrite your uncommitted edits to files that differ between the commits. Uncommitted edits to other files are carried along. It's a good default when you just want to drop the last commits:
git init -q trail-guide && cd trail-guide
echo "Lake" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Ridge" > ridge.txt; git add .; git commit -q -m "Add ridge page"
echo "Ridge, steep in places" > ridge.txt # uncommitted edit to a file the commit added
git reset --keep HEAD~1
echo "exit status $?"
cat ridge.txt
error: Entry 'ridge.txt' not uptodate. Cannot merge.
fatal: Could not reset index file to revision 'HEAD~1'.
exit status 128
Ridge, steep in places
--hard would have deleted ridge.txt along with the edit. --keep stopped instead, with the terse message "Entry 'ridge.txt' not uptodate", meaning the file has local changes. Commit or stash them first (article 27). --merge is similar, and is what git merge --abort uses.
Don't reset pushed commits
reset rewrites the branch. If you have already pushed the commits, the remote still has them, a normal push is rejected, and a force push would remove them from everyone else's history, so their next pull duplicates or conflicts (article 14). For pushed commits, use revert.
Undoing a pushed commit with git revert
git revert <commit> creates a new commit that applies the opposite of the given commit's changes. History isn't rewritten: the original commit stays, and the revert commit records that it was undone. That's safe for shared branches:
git init -q trail-guide && cd trail-guide
echo "Lake loop: 5 km" > lake.txt; echo "Ridge loop: 8 km" > ridge.txt; git add .; git commit -q -m "Add trails"
echo "Ridge loop: closed" > ridge.txt; git commit -q -am "Mark ridge loop closed"
echo "Lake loop: 5.4 km" > lake.txt; git commit -q -am "Update lake distance"
git revert --no-edit HEAD~1 # the ridge loop reopened
echo "=== history"
git log --oneline
echo "=== the revert commit's message"
git log -1 --format=%B
cat ridge.txt
[main 308ac23] Revert "Mark ridge loop closed"
Date: Mon Mar 2 10:00:00 2026 +0000
1 file changed, 1 insertion(+), 1 deletion(-)
=== history
308ac23 Revert "Mark ridge loop closed"
c3fc394 Update lake distance
7bd82b4 Mark ridge loop closed
60093b8 Add trails
=== the revert commit's message
Revert "Mark ridge loop closed"
This reverts commit 7bd82b4c090303f03d923c9fb697037be5ac7ce4.
Ridge loop: 8 km
- The revert commit's message is
Revert "<original subject>", withThis reverts commit <id>.in the body. Add the reason, without--no-edit, by editing the message. - Reverting an older commit works as long as later commits didn't change the same lines. Otherwise you get a conflict, resolved as in article 13, then
git revert --continue, or--abortto give up. - Several commits:
git revert <oldest>^..<newest>creates one revert commit for each, newest first. Add--no-commit(-n) to apply all the reversals and commit them once. - Reverting a revert puts the change back. Since Git 2.43, its message reads
Reapply "<original subject>". - A merge commit needs
-m 1to say which parent is the mainline (article 25).
Detached HEAD: looking at old commits
To look at, build or test the project as it was at an old commit, check out the commit itself rather than a branch:
git switch --detach a1b2c3d # or: git checkout a1b2c3d
git switch --detach v1.0 # a tag works too
That puts you in detached HEAD state (article 11): HEAD points directly to a commit, not to a branch. Nothing is being undone, and every branch is unchanged. You can run the code, and even make commits:
git init -q trail-guide && cd trail-guide
echo "Lake" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Ridge" > ridge.txt; git add .; git commit -q -m "Add ridge page"
git switch --detach HEAD~1 2>&1 | head -1
ls
echo "Experimental lake map" > map.txt; git add .; git commit -q -m "Try a lake map"
git status | head -1
git switch -c lake-map # keep the experiment on a new branch
git log --oneline --all --graph
HEAD is now at cf8c779 Add lake page
lake.txt
HEAD detached from cf8c779
Switched to a new branch 'lake-map'
* 010cb7c Try a lake map
| * ad382c1 Add ridge page
|/
* cf8c779 Add lake page
Commits made in detached HEAD belong to no branch. Create one with git switch -c <name> before switching away, as above. If you've already switched away, Git prints the lost commit's ID, and the reflog has it (article 26). To leave without keeping anything, just switch back: git switch main, or git switch - for the previous branch.
What's current
git restoreandgit switch(Git 2.23) separate file operations from branch operations. They're the recommended commands;git checkoutstill does both.- Since Git 2.43, reverting a revert writes
Reapply "..."instead ofRevert "Revert "..."". - Git 2.54 and later include the experimental
git historycommand, which can drop (2.56), reword, split and fix up commits without a full interactive rebase (article 15). It rewrites history, so the same "not on pushed commits" rule applies.
Common questions
What's the difference between reset and revert?
reset moves your branch back, removing commits from it: rewriting history, fine for local commits. revert adds a new commit that undoes an old one: history is kept, which is safe for pushed commits.
I ran git reset --hard and lost my changes. Can I get them back?
Committed work, yes: git reset --hard ORIG_HEAD, or find the commit in git reflog (article 26). Uncommitted edits that were never staged, no. Edits that were staged but not committed may be recoverable with git fsck --lost-found (article 26).
How do I undo git add?
git restore --staged <file>, or git reset <file>. Your edits stay in the file.
How do I undo the last commit but keep the changes?
git reset --soft HEAD~1 keeps them staged, and git reset HEAD~1 keeps them unstaged. To change only the message or add a forgotten file, git commit --amend is simpler (article 6).
What does "You are in 'detached HEAD' state" mean? Did I break something?
No. You checked out a commit or tag instead of a branch. Look around freely. Before committing anything you want to keep, create a branch with git switch -c <name>. Otherwise git switch main takes you back.
Summary
git restore <file>discards unstaged edits,--stagedunstages, and--source=<commit>brings back an older or deleted version.git reset <commit>moves the current branch:--softkeeps changes staged,--mixed(default) unstaged, and--harddiscards them, including uncommitted edits.--keeprefuses to overwrite uncommitted edits.ORIG_HEADand the reflog undo a reset of committed work.git revert <commit>adds a commit that reverses an earlier one: the safe undo for pushed history.git switch --detach <commit>lets you inspect or experiment on an old commit. Save work there withgit switch -c <branch>.
Practice questions
You staged three files, but one, notes.txt, shouldn't be in this commit. How do you take it out without losing its edits?
Answer
git restore --staged notes.txt (or git reset notes.txt). The file goes back to unstaged, with your edits intact, and the other two stay staged.
You made four local commits that should have been one. They haven't been pushed. What's a quick way to combine them?
Answer
git reset --soft HEAD~4, then git commit. The soft reset moves the branch back four commits but keeps all their changes staged, so one new commit contains them all. Interactive rebase with fixup does the same (article 15).
A commit with a bug was pushed to main yesterday, and others have pulled it. How do you undo it?
Answer
git revert <commit>, then push. The revert commit reverses the bug's changes without rewriting history, so teammates simply pull it. Resetting and force pushing would rewrite a shared branch and disrupt everyone who has the commit.
Previous: Tidying Up: Pruning Remote-Tracking Branches and Deleting Merged and Gone Branches
Next: Undoing Merges: Aborting, Resetting and Reverting a Merge Commit

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.