How to Undo Changes in Git
A letter can be taken back at many points. While you are still writing it, you just cross out a line. Once it is in the envelope, you open the envelope. Once it is in your own post box, you take it out again. But once the postman has delivered it, you can't take it back: you send a second letter that says "please ignore my last one".
Undoing in Git works the same way. Which command you need depends on how far the change has traveled: still being edited, staged, committed, or pushed to others. The earlier the stage, the simpler and more private the undo. Once a change is shared, the only safe undo is a new commit that reverses it.
This lesson is the map. It shows you how to find where your change is, gives you the right command for each case in one table, and shows each one briefly. The lessons that follow cover each command in depth.
Where Is Your Change?
git status and git log tell you where a change is:
| What you see | Where the change is |
|---|---|
?? in git status -s | An untracked file, never added |
M (space, then M) | Edited, not staged |
M (M, then space) | Staged, not committed |
The commit is in git log, and not pushed | Committed on your computer only |
The commit is in git log and on the remote | Pushed: others may already have it |
The Decision Table
| I want to... | The change is | Command | Can I get it back afterward? |
|---|---|---|---|
| Throw away my edits to a file | Edited | git restore <file> | No |
| Unstage a file, keep the edits | Staged | git restore --staged <file> | Nothing is lost |
| Unstage and throw away | Staged | git restore -SW <file> | No |
| Fix the last commit's message or add a file | Committed, not pushed | git commit --amend | Yes, with the reflog |
| Undo commits, keep the changes | Committed, not pushed | git reset --soft HEAD~1 or git reset HEAD~1 | Nothing is lost |
| Undo commits and their changes | Committed, not pushed | git reset --hard HEAD~1 | Yes, with the reflog |
| Undo a commit others already have | Pushed | git revert <commit> | Yes, revert the revert |
| Remove untracked files | Untracked | git clean -n, then git clean -f | No |
| Get back a commit after a bad reset | Anywhere | git reflog, then git reset --hard HEAD@{n} | Not needed: this is the recovery |
A Quick Tour
Here is each case once, in one small project. Every command has its own lesson with the details.
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
echo 'Term 1 fee: $12,000' > fees.txt
git add .
git commit --quiet -m "Add README and the term fee"
git log --oneline4988b51 (HEAD -> main) Add README and the term feeEdited, not staged: throw the edit away.
echo 'Term 1 fee: $1' >> fees.txt
git status -s
git restore fees.txt
git status -s
cat fees.txt M fees.txt
Term 1 fee: $12,000Staged: take it out of the staging area, keep the edit.
echo 'Late fee: $250' >> fees.txt
git add fees.txt
git status -s
git restore --staged fees.txt
git status -sM fees.txt
M fees.txtCommitted with a typing mistake, not pushed: amend.
git commit --quiet -am "Add the lat fee"
git commit --quiet --amend -m "Add the late fee"
git log --onelinec3f07b8 (HEAD -> main) Add the late fee
4988b51 Add README and the term feeCommitted, not pushed, and not wanted: reset. --soft and --mixed keep the changes, --hard throws them away:
echo "Run the app to see the fees." >> README.md
git commit --quiet -am "Explain how to run the app"
git reset --quiet --soft HEAD~1
git status -s
git log --onelineM README.md
c3f07b8 (HEAD -> main) Add the late fee
4988b51 Add README and the term feeThe README commit is gone from the history, and its change is staged, ready to commit again or to change.
Pushed: revert. Pretend the late fee commit is already pushed. Navika commits the README change again, then reverts the late fee, without rewriting anything:
git commit --quiet -m "Explain how to run the app"
git revert --no-edit HEAD~1
git log --oneline
cat fees.txt[main 126e4f2] Revert "Add the late fee"
Date: Mon Jan 5 10:33:00 2026 -0600
1 file changed, 1 deletion(-)
126e4f2 (HEAD -> main) Revert "Add the late fee"
2dfac48 Explain how to run the app
c3f07b8 Add the late fee
4988b51 Add README and the term fee
Term 1 fee: $12,000Untracked: clean. Preview with -n, then remove with -f:
echo "scratch" > scratch.txt
git clean -n
git clean -f
git status -sWould remove scratch.txt
Removing scratch.txtLost a commit with git reset --hard: reflog.
git reset --quiet --hard HEAD~2
git log --oneline
git reflog -2
git reset --quiet --hard HEAD@{1}
git log --onelinec3f07b8 (HEAD -> main) Add the late fee
4988b51 Add README and the term fee
c3f07b8 (HEAD -> main) HEAD@{0}: reset: moving to HEAD~2
126e4f2 HEAD@{1}: revert: Revert "Add the late fee"
126e4f2 (HEAD -> main) Revert "Add the late fee"
2dfac48 Explain how to run the app
c3f07b8 Add the late fee
4988b51 Add README and the term feeThe hard reset took two commits off main, the reflog showed where HEAD was before, and a reset to HEAD@{1} put them back.
Restore vs Reset vs Revert
The three names sound alike, and they are the most often confused commands in Git.
git restore | git reset | git revert | |
|---|---|---|---|
| Works on | Files | The branch (and files) | Commits |
| Changes history? | Never | Yes: moves the branch back | No: adds a new commit |
| Typical use | Throw away edits, unstage | Undo local commits | Undo a pushed commit |
| Safe after push? | Yes (it doesn't touch commits) | No | Yes |
| Older form | git checkout -- <file> | None | None |
A short way to remember them: restore files, reset a branch, revert a commit.
Common Mistakes
Using git reset on something already pushed. Use git revert. A reset on shared history makes your branch disagree with everyone else's.
Reaching for --hard when you only wanted to unstage. git restore --staged or git reset <file> unstages and keeps your edits.
Deleting the repository and cloning again. Almost every mistake has a one-line fix in the table above. A fresh clone loses everything that isn't pushed, including your local branches and stashes.
Not checking git status first. The right command depends on where the change is. One look tells you.
Undoing in a panic. Most undos can themselves be undone, through the reflog. The three that can't are listed in the warning above. Slow down for those.
Under the Hood
Every undo command copies content between the same places you met in "Working Tree, Staging Area and Repository", or moves a branch:
| Command | What it changes |
|---|---|
git restore <file> | Staging area → working tree |
git restore --staged <file> | HEAD → staging area |
git reset --soft <c> | Moves the branch to <c> |
git reset --mixed <c> | Moves the branch, then <c> → staging area |
git reset --hard <c> | Moves the branch, then <c> → staging area → working tree |
git revert <c> | Adds a new commit with the opposite of <c> |
git clean -f | Deletes untracked files from the working tree |
Commits are never really deleted by these commands. A commit that is no longer on any branch stays in the repository, reachable through the reflog, until Git's housekeeping removes it, by default after at least 30 days. That is what makes committed work so hard to lose, and why "commit early" is good advice even for work in progress.
Interview Corner
How do you undo changes in Git?
It depends on where the change is. Unstaged edits: git restore <file>. Staged: git restore --staged <file>. A local commit: git commit --amend for the last one, or git reset (--soft, --mixed, --hard). A pushed commit: git revert. Untracked files: git clean. Lost commits: git reflog.
What is the difference between git restore, git reset and git revert?
git restore changes files in the working tree or staging area and never touches commits. git reset moves the current branch to another commit, rewriting local history. git revert adds a new commit that reverses an earlier one, which is safe for shared history.
Which undo operations can't be recovered?
Those that destroy uncommitted work: git restore on unstaged changes, git reset --hard with uncommitted changes, git clean -f, and git checkout -- <file>. Committed work can almost always be recovered through the reflog.
You committed to the wrong branch and haven't pushed. What do you do?
Create a branch where you are (git branch <name>), so the commit is kept there, then move the current branch back with git reset --hard HEAD~1. If the right branch already exists, switch to it and git cherry-pick the commit, then reset the wrong branch.
Practice
Challenge: pick the command. In the same repository, three things are wrong at once: README.md has an unwanted edit that is not staged, fees.txt has a staged change you want to keep but not commit yet, and an untracked file temp.txt should go. Fix all three with the commands from the decision table, then print the short status.
What does Git print? Before you open the solution, choose a command for each file, and predict the one line that remains.
Show solution
echo "oops" >> README.md
echo 'Exam fee: $800' >> fees.txt
git add fees.txt
echo "temp" > temp.txt
git restore README.md
git restore --staged fees.txt
git clean -f temp.txt
git status -sRemoving temp.txt
M fees.txtgit restore README.md threw away the unwanted edit, git restore --staged fees.txt unstaged the exam fee and kept it in the file, and git clean -f temp.txt removed the untracked file. The only line left is M fees.txt: an edit that is kept but not staged.
Checkpoint. Do Lab 6 from the course labs: in a practice repository, create each of six "oops" situations (a bad edit, a wrong file staged, a typing mistake in the last commit, an unwanted local commit, an unwanted pushed commit, and a lost commit after reset --hard), and fix each one with the command from the decision table.
Key Takeaways
Quick reference
| Situation | Command |
|---|---|
| Throw away unstaged edits | git restore <file> |
| Unstage, keep the edits | git restore --staged <file> |
| Fix the last commit | git commit --amend |
| Undo local commits, keep the changes | git reset --soft HEAD~1 / git reset HEAD~1 |
| Undo local commits and their changes | git reset --hard HEAD~1 |
| Undo a pushed commit | git revert <commit> |
| Remove untracked files | git clean -n, then git clean -f |
| Find a lost commit | git reflog |
Was this tutorial helpful?