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
The working folder
The files as they are on disk. Edits here have been saved by your editor, and Git has not recorded them.
The staging area
What you have marked with
git addto go into the next commit.The history
Commits. On your machine only, or pushed and shared with others.
Each place has its own way back.
Find your command
Where is the thing you want to undo?
Undoing edits you have not committed
Throw away changes to a file:
git restore notes.txtThe file goes back to how it was at the last commit.
Unstage a file, keeping the edits:
git restore --staged notes.txtIt 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.
$
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
git add forgotten-file.txt
git commit --amendUse it for a typo in the message or a file you forgot. It replaces the last commit with a corrected one.
Go back: reset
git reset --soft HEAD~1 # undo the commit, keep the changes staged
git reset --hard HEAD~1 # undo the commit and discard the changesreset moves the branch pointer backwards. HEAD~1 means "one commit before where I am now".
--softleaves your work staged, ready to commit again.--mixed(the default) leaves your work in the files, unstaged.--hardmakes your files match the commit you went back to.
Cancel it out: revert
git revert c3f1a9revert 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 is | Use |
|---|---|
| Only on your machine | amend or reset: tidy up freely |
| Pushed to a shared branch | revert: 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.
git refloga1c94f0 HEAD@{0}: reset: moving to HEAD~2
c3f1a92 HEAD@{1}: commit: break everything
b2e77d1 HEAD@{2}: commit: fix typoEach line is somewhere you have been, with its commit id. To go back:
git reset --hard b2e77d1 # put the branch back there
git branch rescue c3f1a92 # or keep it safe on a new branchSelect "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 to | Command |
|---|---|
| Discard edits to a file | git restore <file> |
| Unstage a file | git restore --staged <file> |
| Set changes aside for later | git stash, then git stash pop |
| Fix the last commit's message or contents | git commit --amend |
| Undo the last commit, keep the changes | git reset --soft HEAD~1 |
| Undo the last commit, discard the changes | git reset --hard HEAD~1 |
| Undo a commit that has been pushed | git revert <commit> |
| Get one file back from an old commit | git restore --source=<commit> <file> |
| Find commits that seem lost | git 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.