How to Undo Changes in Git

Updated · 11 min read
easyGit 2.53gitundorestoreresetrevertinterview

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 seeWhere the change is
?? in git status -sAn 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 pushedCommitted on your computer only
The commit is in git log and on the remotePushed: others may already have it

The Decision Table

I want to...The change isCommandCan I get it back afterward?
Throw away my edits to a fileEditedgit restore <file>No
Unstage a file, keep the editsStagedgit restore --staged <file>Nothing is lost
Unstage and throw awayStagedgit restore -SW <file>No
Fix the last commit's message or add a fileCommitted, not pushedgit commit --amendYes, with the reflog
Undo commits, keep the changesCommitted, not pushedgit reset --soft HEAD~1 or git reset HEAD~1Nothing is lost
Undo commits and their changesCommitted, not pushedgit reset --hard HEAD~1Yes, with the reflog
Undo a commit others already havePushedgit revert <commit>Yes, revert the revert
Remove untracked filesUntrackedgit clean -n, then git clean -fNo
Get back a commit after a bad resetAnywheregit reflog, then git reset --hard HEAD@{n}Not needed: this is the recovery
Which undo do I need?

A Quick Tour

Here is each case once, in one small project. Every command has its own lesson with the details.

Terminal
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 --oneline
Output
4988b51 (HEAD -> main) Add README and the term fee

Edited, not staged: throw the edit away.

Terminal
echo 'Term 1 fee: $1' >> fees.txt
git status -s
git restore fees.txt
git status -s
cat fees.txt
Output
 M fees.txt
Term 1 fee: $12,000

Staged: take it out of the staging area, keep the edit.

Terminal
echo 'Late fee: $250' >> fees.txt
git add fees.txt
git status -s
git restore --staged fees.txt
git status -s
Output
M  fees.txt
 M fees.txt

Committed with a typing mistake, not pushed: amend.

Terminal
git commit --quiet -am "Add the lat fee"
git commit --quiet --amend -m "Add the late fee"
git log --oneline
Output
c3f07b8 (HEAD -> main) Add the late fee
4988b51 Add README and the term fee

Committed, not pushed, and not wanted: reset. --soft and --mixed keep the changes, --hard throws them away:

Terminal
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 --oneline
Output
M  README.md
c3f07b8 (HEAD -> main) Add the late fee
4988b51 Add README and the term fee

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

Terminal
git commit --quiet -m "Explain how to run the app"
git revert --no-edit HEAD~1
git log --oneline
cat fees.txt
Output
[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,000

Untracked: clean. Preview with -n, then remove with -f:

Terminal
echo "scratch" > scratch.txt
git clean -n
git clean -f
git status -s
Output
Would remove scratch.txt
Removing scratch.txt

Lost a commit with git reset --hard: reflog.

Terminal
git reset --quiet --hard HEAD~2
git log --oneline
git reflog -2
git reset --quiet --hard HEAD@{1}
git log --oneline
Output
c3f07b8 (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 fee

The 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 restoregit resetgit revert
Works onFilesThe branch (and files)Commits
Changes history?NeverYes: moves the branch backNo: adds a new commit
Typical useThrow away edits, unstageUndo local commitsUndo a pushed commit
Safe after push?Yes (it doesn't touch commits)NoYes
Older formgit checkout -- <file>NoneNone

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:

CommandWhat 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 -fDeletes 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
Terminal
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 -s
Output
Removing temp.txt
 M fees.txt

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

Quick check: Undoing changes

4 questions
1Your edit to fees.txt is not staged and you want it gone. Which command?
2You need to undo a commit that is already on GitHub and pulled by teammates. Which command?
3Which of these can be recovered with git reflog?
4Which command never changes any commit?

Key Takeaways

Quick reference

SituationCommand
Throw away unstaged editsgit restore <file>
Unstage, keep the editsgit restore --staged <file>
Fix the last commitgit commit --amend
Undo local commits, keep the changesgit reset --soft HEAD~1 / git reset HEAD~1
Undo local commits and their changesgit reset --hard HEAD~1
Undo a pushed commitgit revert <commit>
Remove untracked filesgit clean -n, then git clean -f
Find a lost commitgit reflog

Was this tutorial helpful?

Lesson 22 of 106