Skip to content
$ lynuxpro
← Git

LESSON 5 OF 21 · BEGINNER · 20 MIN

Undoing changes

HOW IT WORKSUndo at every stage

git restoreWorking treeedited app.pyStaging areastagedLast commitHEADPushed commitsharedNew revert commitsafe undo
1/5

Throw away edits you have not staged: restore the file from the staging area.

$ git restore app.py

Everyone makes mistakes: an edit you regret, the wrong file staged, a typo in a commit message, a commit that broke production. Git can undo almost anything, but the right command depends on where the mistake is (working tree, staging area, a local commit or a pushed commit). This lesson gives you a clear map so you always pick the safe option.

In simple words

  • git restore: throws away edits to a file, or takes a file back out of the staging area.
  • --amend: fixes the last commit: its message or forgotten files.
  • git reset: moves your branch back to an earlier commit. Only for commits you have not shared.
  • git revert: makes a new commit that undoes an old one. The safe way to undo something already pushed.

The map

MistakeCommandSafe?
Edited a file, want the last committed version backgit restore filediscards your edits
Staged a file by accidentgit restore --staged fileyes, keeps edits
Typo in the last commit message, or forgot a filegit commit --amendonly if not pushed
Undo the last commit, keep the changesgit reset --soft HEAD~1only if not pushed
Undo the last commit, keep changes unstagedgit reset HEAD~1only if not pushed
Throw away commits and changesgit reset --hard HEAD~1destructive
Undo a commit that is already pushedgit revert <commit>yes, always

The rule behind it: never rewrite commits that others already have. Locally you can rewrite freely; once pushed, add a new commit that undoes the old one instead.

Set up a small repository to practise on:

git config --global user.name "Leela Kumar"mkdir undo-labcd undo-labgit initprintf 'TAX_RATE = 0.18\n' > pricing.pyprintf '# Shop\n' > README.mdgit add .git commit -m "Add pricing and README"
SHELLbashutf-8 · 8L

1. Discard edits in the working tree: git restore

printf 'TAX_RATE = 99\n' > pricing.pygit status -sgit restore pricing.pycat pricing.pygit status -s
SHELLbashutf-8 · 5L
 M pricing.pyTAX_RATE = 0.18
OUTPUTterminalutf-8 · 2L

git restore file replaces the file with its staged (or last committed) version. Your edits are gone for good, because they were never committed, so double-check with git diff first. Restore a whole folder with git restore src/ or everything with git restore ..

You may see the older git checkout -- file in tutorials. It does the same thing; git restore was added in Git 2.23 to make the intent clearer.

2. Unstage a file: git restore --staged

You ran git add . and staged something you did not mean to commit:

printf 'DEBUG=true\n' > debug.envprintf '\nRun with: flask run\n' >> README.mdgit add .git status -sgit restore --staged debug.envgit status -s
SHELLbashutf-8 · 6L
M  README.mdA  debug.envM  README.md?? debug.env
OUTPUTterminalutf-8 · 4L

The file left the staging area but your work is untouched. (The older equivalent is git reset HEAD file.)

3. Fix the last commit: --amend

Commit, then notice a typo in the message and a forgotten file:

git commit -m "Updte README"rm debug.envprintf 'flask==3.1.0\n' > requirements.txtgit add requirements.txtgit commit --amend -m "Update README and add requirements"git log --oneline
SHELLbashutf-8 · 6L
[main 6b1d0e2] Updte README 1 file changed, 2 insertions(+)[main 9c4a7f3] Update README and add requirements 2 files changed, 3 insertions(+) create mode 100644 requirements.txt9c4a7f3 (HEAD -> main) Update README and add requirements3e2f1a8 Add pricing and README
OUTPUTterminalutf-8 · 7L

--amend replaces the last commit with a new one (note the new hash) containing the staged changes and the new message. Use git commit --amend --no-edit to add files without changing the message.

Because amend creates a different commit, never amend something you have already pushed to a shared branch.

4. Move a branch back: git reset

git reset <commit> moves the current branch to an earlier commit. Its mode decides what happens to the changes from the commits you "undo":

ModeBranch movesStaging areaWorking tree
--softyeskeeps changes stageduntouched
--mixed (default)yesreset; changes become unstageduntouched
--hardyesresetreset: changes deleted

Try --soft, which is perfect when you committed too early and want to re-do the commit:

printf 'def total(x):\n    return x * 1.18\n' >> pricing.pygit commit -am "Add total"git reset --soft HEAD~1git status -sgit log --oneline
SHELLbashutf-8 · 5L
M  pricing.py9c4a7f3 (HEAD -> main) Update README and add requirements3e2f1a8 Add pricing and README
OUTPUTterminalutf-8 · 3L

The commit is gone from history, but its change is still staged, ready to commit again.

--hard is the dangerous one. It deletes uncommitted work without asking:

git reset --hard HEADcat pricing.py
SHELLbashutf-8 · 2L
HEAD is now at 9c4a7f3 Update README and add requirementsTAX_RATE = 0.18
OUTPUTterminalutf-8 · 2L

The staged total function is gone. If you reset away commits by accident, git reflog can usually bring them back (lesson 15), but uncommitted changes lost with --hard are gone forever.

5. Undo a pushed commit safely: git revert

When a bad commit is already on the shared main branch, others have it. Rewriting history would break their copies. Instead, git revert creates a new commit that applies the opposite change:

printf 'TAX_RATE = 1.8\n' > pricing.pygit commit -am "Change tax rate"git revert HEAD --no-editgit log --oneline -n 3cat pricing.py
SHELLbashutf-8 · 5L
[main 5a8e2c1] Change tax rate 1 file changed, 1 insertion(+), 1 deletion(-)[main 0d7b3f9] Revert "Change tax rate" 1 file changed, 1 insertion(+), 1 deletion(-)0d7b3f9 (HEAD -> main) Revert "Change tax rate"5a8e2c1 Change tax rate9c4a7f3 Update README and add requirementsTAX_RATE = 0.18
OUTPUTterminalutf-8 · 8L

History keeps both the mistake and its fix, which is exactly what you want on a shared branch: an honest audit trail and no surprises for teammates. You can revert any commit, not just the last one: git revert 5a8e2c1.

Restoring one file from an older commit

Sometimes you need just one file as it was:

git restore --source=HEAD~1 pricing.pycat pricing.pygit restore pricing.py
SHELLbashutf-8 · 3L
TAX_RATE = 1.8
OUTPUTterminalutf-8 · 1L

--source takes any commit, branch or tag. The result is a normal uncommitted change you can inspect, commit or discard.

Real-world scenario: the hotfix that made it worse

A hotfix merged to main at 16:00 introduced a new bug. The team's rule is "revert first, investigate second". The on-call engineer ran:

git switch main && git pullgit revert --no-edit 7c1e2d4git push
SHELLbashutf-8 · 3L

The pipeline redeployed in minutes, with the revert commit clearly linked to the bad one in history. Later, the original author fixed the bug properly and reverted the revert (git revert <revert-commit>) together with the fix. Nobody had to force-push, and nobody's local copy broke.

Common mistakes

  • git reset --hard with uncommitted work you wanted. Run git status and git stash first if unsure.
  • Amending or resetting pushed commits, then force-pushing over teammates' work.
  • Using reset to undo on shared branches instead of revert.
  • Forgetting that git restore file cannot be undone for uncommitted edits.
  • Confusing revert (new opposite commit) with reset (move the branch).

Practice

  1. Edit a file and discard the edit with git restore.
  2. Stage two files, then unstage one with git restore --staged.
  3. Make a commit with a typo in the message and fix it with --amend.
  4. Make a commit and undo it with git reset --soft HEAD~1; check that the change is still staged.
  5. Make a commit and undo it with git revert, then look at git log --oneline.

Cheat sheet

git restore file                  discard working-tree editsgit restore --staged file         unstage, keep editsgit restore --source=HEAD~2 file  file from an older commitgit commit --amend [-m msg]       replace last commit (not pushed!)git commit --amend --no-edit      add staged files to last commitgit reset --soft HEAD~1           undo commit, keep stagedgit reset HEAD~1                  undo commit, keep as unstaged editsgit reset --hard HEAD~1           undo commit and delete changesgit revert <commit> [--no-edit]   new commit that undoes one (safe when pushed)
OUTPUTterminalutf-8 · 9L

Key takeaways

  • Choose the undo by where the mistake is: working tree, staging area, local commit or pushed commit.
  • git restore handles files; --staged unstages without losing edits.
  • --amend and reset rewrite history: fine locally, never on shared branches.
  • reset --soft, --mixed and --hard differ in what happens to your changes; --hard deletes them.
  • git revert is the safe undo for anything already pushed.