Everyone makes mistakes in Git: committing to the wrong branch, staging the wrong file, writing a bad commit message, or deleting a branch that still had work on it. The good news is that Git rarely loses anything that was ever committed, and most mistakes can be undone in one or two commands, if you pick the right one. This lesson sorts the undo commands by what they change and how dangerous they are, and shows the reflog, Git's safety net, recovering work that looks lost.
Before you start
You should be comfortable with the working tree, staging area, and commits from Git Fundamentals, and with remotes from lesson 5. Practice every command here in a throwaway repository before you need it for real.
First, two safety rules
- Uncommitted work is the easiest to lose. Commands that overwrite the working tree (
git restore,git reset --hard,git checkout -- file,git clean) can discard edits that Git never recorded, and those can't be recovered from Git. When in doubt, commit or stash first. - Don't rewrite history others already have. Commands that replace commits (
--amend,reset, rebase) are fine on your own unpushed work. On commits already pushed to a shared branch, usegit revert, which adds a new commit instead of removing old ones.
Before any risky command, a cheap backup is one line:
git branch backup/before-cleanupWhich command undoes what
Think about what you want to undo:
- Changes in a file you haven't staged →
git restore <file> - Changes you staged by mistake →
git restore --staged <file> - The last commit's message or contents, not yet pushed →
git commit --amend - Recent commits, not yet pushed →
git reset - A commit already pushed to a shared branch →
git revert - Something you deleted or reset by mistake →
git reflog
Restoring files: git restore
git restore, available since Git 2.23, does one thing: restores file contents.
git restore --staged notes.md # unstage: keep your edits, remove them from the next commit
git restore notes.md # discard unstaged edits: the file goes back to the staged version
git restore --source=HEAD~2 notes.md # bring back the version from two commits agoHere, an unwanted line was added and staged. Unstaging keeps the edit in the working tree; restoring then discards it:
$ git restore --staged notes.md
$ git status --short
M notes.md
$ git restore notes.md
$ git status --shortThe second command really discards the edit; it was never committed, so there's no history to recover it from. The older git checkout -- notes.md does the same and is just as final.
Fixing the last commit: git commit --amend
Forgot a file, or made a typo in the message? If you haven't pushed yet:
git add forgotten-file.ts
git commit --amend -m "Add rounding for free carts"--amend doesn't edit the old commit; it creates a new one and moves the branch to it. The old commit still exists for a while (the reflog can find it). If you had already pushed the old one, your branch and the remote now differ, which is why amending pushed commits on shared branches causes trouble.
Moving a branch backward: git reset
git reset <commit> moves the current branch to another commit. Its three modes differ in what happens to your files:
--soft: moves the branch only. The undone commits' changes stay staged. Use it to redo a set of commits as one.--mixed(the default): moves the branch and unstages. The changes stay in your working tree, unstaged.--hard: moves the branch and makes the staging area and working tree match the commit. Changes from the undone commits, and any uncommitted edits, disappear from your files.
git reset --soft HEAD~1 # undo the last commit, keep its changes staged
git reset HEAD~1 # undo the last commit, keep its changes unstaged
git reset --hard HEAD~2 # discard the last two commits and all uncommitted editsHEAD~1 means "one commit before HEAD." You can also name any commit ID.
Committed to the wrong branch
A common rescue: you made two commits on main that belonged on a feature branch, and haven't pushed.
git branch feature/search # a new branch pointing at the current commit (keeps the work)
git reset --hard HEAD~2 # move main back two commits
git switch feature/search # continue thereRun git status first to be sure nothing uncommitted would be lost by --hard.
Undoing shared history: git revert
When a bad commit is already on a shared branch, don't remove it; revert it. git revert creates a new commit that applies the opposite of the original change:
$ git revert --no-edit HEAD
[main c3ddd0d] Revert "Add line 3"
1 file changed, 1 deletion(-)
$ git log --oneline -2
c3ddd0d Revert "Add line 3"
4c921e5 Add line 3History stays intact and everyone else can pull normally. To revert a merge commit, tell Git which parent is the mainline, usually the first: git revert -m 1 <merge-commit>. Reverting a merge means its changes won't come back if you merge the same branch again later, unless you revert the revert, so note it in the commit message.
The reflog: Git's safety net
The reflog records every position HEAD and each branch have had: commits, resets, switches, rebases, and amends, kept locally for a while (by default, entries stay at least 30 days for unreachable commits and 90 days otherwise). Here, a reset --hard threw away two commits:
$ git reset --hard HEAD~2
HEAD is now at a76fd81 Add line 2
$ git reflog -5
a76fd81 HEAD@{0}: reset: moving to HEAD~2
c3ddd0d HEAD@{1}: revert: Revert "Add line 3"
4c921e5 HEAD@{2}: commit: Add line 3
a76fd81 HEAD@{3}: commit: Add line 2
7f91f72 HEAD@{4}: commit (initial): Add line 1The "lost" commits are listed. Moving back recovers them:
$ git reset --hard HEAD@{1}
HEAD is now at c3ddd0d Revert "Add line 3"Recovering a deleted branch
Force-deleting a branch with unmerged work prints the commit it pointed to. If you missed that, the reflog still has it:
$ git branch -D experiment
Deleted branch experiment (was 67fae27).
$ git reflog | grep -m1 "Experiment"
67fae27 HEAD@{1}: commit: Experiment
$ git branch experiment 67fae27 # the branch is backThe reflog is local: it records what happened in your repository, not on the server or in a teammate's clone. And it can't recover edits that were never committed.
Cleaning untracked files: git clean
git clean deletes untracked files, such as build artifacts. It's permanent, so always preview first:
git clean -n # dry run: list what would be deleted
git clean -fd # delete untracked files and directoriesAdd -x only if you also want ignored files gone, such as node_modules/ or .env, which is rarely what you want.
When a mistake reached the remote
- A bad change on a shared branch:
git revertit and push the revert. - A commit on your own feature branch that nobody else uses: you may rewrite it and push with
git push --force-with-lease, which refuses to overwrite the remote if someone else pushed in the meantime. Never force-push a shared branch such asmain. - A secret was pushed: rewriting history isn't enough; the secret was exposed the moment it was pushed. Revoke it first, then clean history (lesson 12).
Common mistakes and troubleshooting
- Using
reset --hardwith uncommitted edits. Those edits are gone for good. Commit, stash, or branch first. - Rewriting pushed history on a shared branch. Use
git revertinstead. - Confusing
revertwith "restore the old version".revertundoes one commit's change, not "go back to how things were on Tuesday." - Panicking after a bad reset or rebase. Run
git reflog; the old commits are almost always there. git clean -fdxwithout a dry run. It deletes ignored files too, such as local configuration.- "detached HEAD" after checking out an old commit. You're viewing history, not on a branch. To keep work made there, create a branch:
git switch -c rescue.
Practical exercise: break things safely
In a throwaway repository with five commits:
- Edit a file, stage it, unstage it with
git restore --staged, then discard it withgit restore. - Make a commit with a typo in the message and fix it with
--amend. - Make two commits on
main, then move them to a new branch as shown above. - Revert one older commit and read the result with
git show. - Run
git reset --hard HEAD~3, then recover the commits withgit reflog. - Create a branch with one commit, force-delete it, and recover it from the reflog.
- Create build junk files and remove them with
git clean, previewing first.
Key takeaways
- Choose the undo command by what you're undoing: files (
restore), the last commit (--amend), local commits (reset), or shared commits (revert). reset --softkeeps changes staged,--mixedkeeps them unstaged, and--harddiscards them along with uncommitted edits.- Never rewrite history others have; revert instead, and use
--force-with-leaseonly on your own branches. - The reflog recovers committed work after resets, amends, rebases, and deleted branches; it can't recover uncommitted edits.
References
- Pro Git: Undoing Things (opens in a new tab)
- Pro Git: Reset Demystified (opens in a new tab)
- Git documentation: git restore (opens in a new tab)
- Git documentation: git revert (opens in a new tab)
- Git documentation: git reflog (opens in a new tab)
- Git documentation: git clean (opens in a new tab)
What's next
With safe undo in your toolbox, you're ready for commands that rewrite history on purpose. Advanced Git Workflows: Rebase, Cherry-Pick, Stash, Tags, and Releases covers them.

