Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 78 ยท The Basic Workflow

Undoing Things: restore, reset and revert Basics

How to undo anything in Git: discard changes with restore, unstage files, move back with reset --soft, --mixed and --hard, and undo published commits with revert.

Everyone makes mistakes with version control: you edit the wrong file, stage too much, commit to the wrong place, or realize that a commit from last week broke the build. Git is very good at undoing things, but it has several undo commands, and they do very different things. Using the wrong one can destroy hours of work. Using the right one is routine.

The key to choosing is to ask where the thing you want to undo lives: only in the working directory, in the index (staging area), in a local commit nobody has seen, or in a commit you have already shared. This lesson gives you the small set of commands (restore, reset, revert and commit --amend) and a decision table you can come back to. The deep dive on reset is in git reset in Depth and the details of revert in Reverting Commits Safely.

Three questions before you undo

  1. Is the work saved anywhere? Changes you made in files but never staged or committed exist nowhere else. Undoing them is permanent. Committed work, on the other hand, is almost always recoverable through the reflog (see The Reflog).
  2. Has anyone else got the commit? If you pushed it, do not rewrite it. Add a new commit that undoes it (git revert).
  3. What is the goal? Throw changes away, or keep them and just rearrange where they sit?

Remember the three areas from How Git Works: the working directory, the index and the repository (HEAD). Every undo command moves data between these three places in a specific direction.

TEXT
   working directory  <-- git restore file ---  index  <-- git restore --staged file ---  HEAD
          |                                       ^                                          ^
          '-------- git add --------------------- ' -------- git commit ---------------------'

Discarding changes in a file: git restore

git restore (added in Git 2.23) copies file contents from somewhere into somewhere else. Called with just a path, it replaces your working file with the version in the index, which discards your unstaged edits.

Bash restore: discard edits and recover a deleted file

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Both the edited file and the deleted file come back exactly as they were in the last snapshot, and the status is clean. Remember that this is one of the few Git commands that loses data for good: whatever you had typed into a.txt after the last git add or commit is gone, and there is no undo for the undo.

git restore <path> and git checkout -- <path> overwrite your working files without asking and without any way back. If you are not sure, copy the file first, or use git stash (see Stashing Work in Progress), which keeps your changes safe.

Unstaging: git restore --staged

With --staged, restore works on the index instead: it copies the version from HEAD into the index and leaves your working file untouched. This is "I ran git add too soon".

You can combine both flags to wipe a file completely. And --source lets you take the content from any commit instead of the default, so you can pull one file out of history without moving your branch.

Bash restore --staged and restore --source

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Step by step: --staged removed the v3 snapshot from the index but left the scribbles. --source=HEAD~1 pulled the version from one commit back (v1) into the working directory; the file now differs from HEAD, so it shows as modified. The last command asked for both targets (--staged --worktree) from HEAD, putting the index and the file back in line with the last commit. A plain git restore file uses the index as the source and the working tree as the target.

Moving the branch back: git reset

git restore changes files. git reset moves the branch pointer (and HEAD with it) to a different commit, and optionally updates the index and working directory to match. It has three modes. The easiest way to remember them is how far the change travels:

Mode Moves the branch Resets the index Resets the working files
--soft Yes No No
--mixed (the default) Yes Yes No
--hard Yes Yes Yes

After git reset --soft HEAD~1, the last commit is undone but all its changes stay staged, so you can immediately commit again with a better message or extra files. After --mixed, the changes are still in your files, but unstaged. After --hard, they are gone from the index and your files: HEAD~1 is the whole truth.

Bash Three kinds of reset on three copies of one history

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

All three modes remove "Third (the mistake)" from the branch history, as git log shows. The difference is what happened to its changes: with --soft the status shows M f.txt (staged), with --mixed it shows M f.txt (unstaged, still three lines in the file) and with --hard nothing is left: the file has two lines and the status is clean. Notice the status for --soft (staged) vs --mixed (unstaged) in the printed output.

Usual uses:

  • Redo the last commit (wrong message, forgot something): git reset --soft HEAD~1, fix up, commit again. (git commit --amend is the shorter way for the very last commit.)
  • Squash the last few commits into one: git reset --soft HEAD~3, then a single git commit.
  • Unstage everything: git reset (mixed, to HEAD).
  • Throw everything away and match the last commit: git reset --hard. Dangerous, as it discards uncommitted work.

Reset is recoverable, if you committed

Because reset only moves a pointer, the "removed" commit still exists in the object database for a while. The reflog records where your branch used to point, so a mistaken reset --hard of committed work can be reversed.

Bash Recovering from a hard reset

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The reflog line HEAD@{1} means "where HEAD was one move ago". Resetting to it brings the lost commit back. This safety net exists only for work that was committed. Edits that were never staged or committed have no copy anywhere. Details are in The Reflog and Recovering Lost Work.

Undoing a commit by adding a commit: git revert

reset rewrites where your branch points, which is fine for local, private commits. If the commit is already pushed and others have built on it, rewriting history causes trouble for them. git revert takes the opposite approach: it creates a new commit that applies the inverse of an earlier one. History is only ever added to.

Bash Reverting a commit in the middle of history

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The bad commit's file b.txt is gone from the project, but c.txt, which came after it, is untouched. And the history now has an extra entry, Revert "Add b (the bad one)", that documents the undo. That is the main advantage: it is honest, safe to push, and itself revertable. If the revert conflicts with later changes, Git stops and lets you resolve it, just like a merge.

Fixing the last commit: --amend

For the very last commit, git commit --amend is usually the neatest fix (message typo, forgotten file). See Making Good Commits. It is a rewrite, so use it only before pushing.

Which command do I need?

I want to ... Command Safe?
Discard my edits to a file git restore file No, edits are lost forever
Discard all edits to tracked files git restore . No
Unstage a file, keep edits git restore --staged file Yes
Get an old version of one file git restore --source=<commit> file Overwrites that file
Delete untracked files git clean -n, then -f No (see .gitignore and Untracked Files)
Fix the last commit's message or content git commit --amend Local commits only
Undo last commit, keep changes staged git reset --soft HEAD~1 Yes
Undo last commit, keep changes unstaged git reset HEAD~1 Yes
Undo last commit and drop its changes git reset --hard HEAD~1 Recoverable via reflog; loses uncommitted work
Undo a commit that is already pushed git revert <commit> Yes
Get back a lost commit git reflog, then git reset --hard <hash> Yes, for a while
Save changes for later git stash Yes

Working safely

  • Before a risky command, look: git status, git diff, git log --oneline -5.
  • Commit often. A commit is a checkpoint you can go back to; uncommitted work cannot be rescued.
  • If unsure, create a backup branch first: git branch backup. It costs nothing and moves nowhere.
  • Prefer restore (files) and revert (published commits) over reset --hard.
  • Never rewrite history that others have pulled, unless the whole team has agreed.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which command unstages notes.txt but keeps your edits in the file?

    --staged makes restore act on the index. Plain git restore would overwrite your working file with the staged version.
  2. 2. You committed a typo-ridden file ten minutes ago and haven't pushed. You want to redo the commit with the same changes staged. What fits best?

    --soft moves the branch back one commit and keeps the changes staged, ready to commit again.
  3. 3. What is the main difference between git reset and git revert?

    reset rewrites where the branch points, which is unsafe for published commits. revert only adds history.
  4. 4. After git reset --hard HEAD~1 you realize the commit was needed. Where can you find it?

    Reset only moves a pointer. The commit remains in the object database for a while and the reflog gives you its hash.
  5. 5. Which of these loses data that is NOT stored in any commit and cannot be recovered?

    Uncommitted edits exist only in your working directory. restore overwrites them with no copy anywhere.

Summary

  • Decide what to undo by where it lives: working directory, index, a local commit or a published commit.
  • git restore file discards edits (permanent); git restore --staged file unstages; --source takes content from any commit.
  • git reset moves the branch: --soft keeps changes staged, --mixed (default) keeps them unstaged, --hard discards them.
  • git revert adds a new commit that undoes an old one; use it for anything already shared.
  • Committed work can be recovered with git reflog; uncommitted edits cannot.
  • When in doubt, make a backup branch or stash first.

Next: Moving, Renaming and Removing Files