Pointer Walk

Undo Anything in Git

Unstage, amend, revert, reset, restore, reflog: which command undoes which mistake.

Developer ToolsBeginner8 min read

Almost nothing in Git is lost

Git has a reputation for being easy to break. The opposite is closer to the truth. Once something has been committed, it is very hard to destroy by accident. Git keeps commits around for weeks even after nothing points to them.

The confusing part is that there are several different undo commands. Which one you need depends on a single question: where is the mistake right now?

The three places your work can be

  1. The working folder

    The files as they are on disk. Edits here have been saved by your editor, and Git has not recorded them.

  2. The staging area

    What you have marked with git add to go into the next commit.

  3. The history

    Commits. On your machine only, or pushed and shared with others.

Each place has its own way back.

Find your command

What do you want to undo?Interactive

Where is the thing you want to undo?

Undoing edits you have not committed

Throw away changes to a file:

bash
git restore notes.txt

The file goes back to how it was at the last commit.

Unstage a file, keeping the edits:

bash
git restore --staged notes.txt

It comes out of the next commit. The file on disk is untouched.

Undoing commits

There are three tools, and they do quite different things to history.

git reset --hardInteractive
Command

$

a1add loginmain
←
b2fix typomain
←
c3break everythingmain

Dashed: a commit nothing points to any more. Green: a commit this command created.

Start. Three commits. The branch "main" points at the newest one, c3, and c3 is a mistake. It has not been pushed.

Fix the last commit: amend

bash
git add forgotten-file.txt
git commit --amend

Use it for a typo in the message or a file you forgot. It replaces the last commit with a corrected one.

Go back: reset

bash
git reset --soft HEAD~1      # undo the commit, keep the changes staged
git reset --hard HEAD~1      # undo the commit and discard the changes

reset moves the branch pointer backwards. HEAD~1 means "one commit before where I am now".

  • --soft leaves your work staged, ready to commit again.
  • --mixed (the default) leaves your work in the files, unstaged.
  • --hard makes your files match the commit you went back to.

Cancel it out: revert

bash
git revert c3f1a9

revert goes forward. It adds a new commit that undoes the changes of an old one. Nothing is removed from history.

The one rule: has it been pushed?

amend and reset rewrite history. They replace commits or drop them. That is fine for commits that exist only on your machine.

Once a commit has been pushed and someone else may have it, rewriting it causes trouble: their history and yours no longer agree, and the next pull or push becomes a mess.

The commit isUse
Only on your machineamend or reset: tidy up freely
Pushed to a shared branchrevert: add a commit that undoes it

Rewrite private history. Add to public history.

The safety net: reflog

Suppose the worst has happened. A reset --hard went too far. A branch was deleted. A rebase went wrong. The commits no longer show in git log.

They are still there. Every time a branch pointer moves, Git notes it in a private diary called the reflog.

bash
git reflog
text
a1c94f0 HEAD@{0}: reset: moving to HEAD~2
c3f1a92 HEAD@{1}: commit: break everything
b2e77d1 HEAD@{2}: commit: fix typo

Each line is somewhere you have been, with its commit id. To go back:

bash
git reset --hard b2e77d1          # put the branch back there
git branch rescue c3f1a92         # or keep it safe on a new branch

Select "reflog" in the animation above to see it work.

Entries are kept for a few weeks by default. The reflog is local: it records what happened on your machine and is not shared when you push.

The cheat sheet

I want toCommand
Discard edits to a filegit restore <file>
Unstage a filegit restore --staged <file>
Set changes aside for latergit stash, then git stash pop
Fix the last commit's message or contentsgit commit --amend
Undo the last commit, keep the changesgit reset --soft HEAD~1
Undo the last commit, discard the changesgit reset --hard HEAD~1
Undo a commit that has been pushedgit revert <commit>
Get one file back from an old commitgit restore --source=<commit> <file>
Find commits that seem lostgit reflog

Key takeaways

  • Which undo you need depends on where the mistake is: working folder, staging area, or history.
  • git restore discards uncommitted edits, and that is the one truly permanent loss.
  • amend and reset rewrite history, so use them only on commits you have not pushed.
  • revert adds a new commit that cancels an old one, which is safe on shared branches.
  • The reflog remembers where your branches have been, so committed work can nearly always be recovered.

Your progress

Check your progress

0%

  1. Read the page50% of this page

    When you have gone through the page and its animations, tick it off.

  2. Take the quiz50% of this page

    7 questions, from easy to hard. Take it as often as you like: the questions change, and only your best score counts.

Saved on this device only. See all your progress