読み物Git

Level: Intermediate

Undoing Mistakes in Git: Restore, Reset, Revert, and Reflog

Pick the right undo for the job: restore files, amend a commit, reset local history, revert shared history, and recover 'lost' work with the reflog.

Edeeson Opina8 min read

An Intermediate lesson card: lesson 9 of the Git & Version Control series, Undoing Mistakes in Git: Restore, Reset, Revert, and Reflog, with the Miso fox smiling.

What you’ll learn

  • Choose an undo command by what you're undoing
  • Use reset --soft, --mixed, and --hard correctly
  • Revert commits on shared branches instead of rewriting them
  • Recover commits and branches with the reflog
On this page
  1. Before you start
  2. First, two safety rules
  3. Which command undoes what
  4. Restoring files: git restore
  5. Fixing the last commit: git commit --amend
  6. Moving a branch backward: git reset
  7. Committed to the wrong branch
  8. Undoing shared history: git revert
  9. The reflog: Git's safety net
  10. Recovering a deleted branch
  11. Cleaning untracked files: git clean
  12. When a mistake reached the remote
  13. Common mistakes and troubleshooting
  14. Practical exercise: break things safely
  15. Key takeaways
  16. References
  17. What's next

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

  1. 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.
  2. 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, use git revert, which adds a new commit instead of removing old ones.

Before any risky command, a cheap backup is one line:

Shell
git branch backup/before-cleanup

Which 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.

Shell
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 ago

Here, an unwanted line was added and staged. Unstaging keeps the edit in the working tree; restoring then discards it:

Shell
$ git restore --staged notes.md
$ git status --short
 M notes.md
$ git restore notes.md
$ git status --short

The 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:

Shell
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.
Shell
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 edits

HEAD~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.

Shell
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 there

Run 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:

Shell
$ 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 3

History 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:

Shell
$ 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 1

The "lost" commits are listed. Moving back recovers them:

Shell
$ 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:

Shell
$ 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 back

The 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:

Shell
git clean -n     # dry run: list what would be deleted
git clean -fd    # delete untracked files and directories

Add -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 revert it 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 as main.
  • 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 --hard with uncommitted edits. Those edits are gone for good. Commit, stash, or branch first.
  • Rewriting pushed history on a shared branch. Use git revert instead.
  • Confusing revert with "restore the old version". revert undoes 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 -fdx without 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:

  1. Edit a file, stage it, unstage it with git restore --staged, then discard it with git restore.
  2. Make a commit with a typo in the message and fix it with --amend.
  3. Make two commits on main, then move them to a new branch as shown above.
  4. Revert one older commit and read the result with git show.
  5. Run git reset --hard HEAD~3, then recover the commits with git reflog.
  6. Create a branch with one commit, force-delete it, and recover it from the reflog.
  7. 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 --soft keeps changes staged, --mixed keeps them unstaged, and --hard discards them along with uncommitted edits.
  • Never rewrite history others have; revert instead, and use --force-with-lease only on your own branches.
  • The reflog recovers committed work after resets, amends, rebases, and deleted branches; it can't recover uncommitted edits.

References

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.

See every lesson in Git & Version Control: From Fundamentals to Advanced Workflows