Git Restore
Most word processors have an "Undo" button and a "Revert to saved" menu item. Undo steps back one change at a time. Revert to saved throws away everything since you last saved and gives you the saved file back.
git restore is Git's "revert to saved" for single files. It can throw away the edits in a file, take a file out of the staging area, bring back a file you deleted, or give you a file exactly as it was in any earlier commit. It works on files only and never changes your commits, which makes it the safest of Git's undo commands.
In this lesson, you will learn each of these uses, and the one situation where git restore can't be undone.
The Starting Point
Navika's project has three commits:
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
git add README.md
git commit --quiet -m "Add project README"
printf 'Term 1 fee: $12,000\nTerm 2 fee: $12,000\n' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fees"
printf 'Term 1 fee: $12,000\nTerm 2 fee: $12,500\n' > fees.txt
git commit --quiet -am "Correct the term 2 fee"
git log --oneline95a4945 (HEAD -> main) Correct the term 2 fee
fe87780 Add the term fees
dd34d8a Add project READMEThrow Away Your Edits
Navika starts changing fees.txt, then realizes she was editing the wrong file. She wants it back exactly as it was:
echo 'Term 3 fee: $99,999' >> fees.txt
git status -s
git restore fees.txt
git status -s
cat fees.txt M fees.txt
Term 1 fee: $12,000
Term 2 fee: $12,500git restore fees.txt replaced the file in the working tree with the version in the staging area, which here is the same as the last commit. The status is clean again, and the wrong line is gone.
Take a File Out of the Staging Area
Navika stages a change, then decides it belongs in a later commit. --staged unstages the file and keeps the edit in the working tree:
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.txtM (staged) became M (modified, not staged). The new line is still in the file. Only the staging area went back to the last commit.
This is the command git status suggests when you have staged changes, in the line (use "git restore --staged <file>..." to unstage).
Both at Once
To unstage a file and throw away its edits, give both --staged and --worktree (short: -S and -W):
git add fees.txt
git restore --staged --worktree fees.txt
git status -s
cat fees.txtTerm 1 fee: $12,000
Term 2 fee: $12,500The file is back to the last commit in both places. The same warning applies: the late fee line is gone for good.
Bring Back a Deleted File
If you delete a tracked file by mistake, git restore brings it back from the staging area:
rm README.md
git status -s
git restore README.md
git status -s
cat README.md D README.md
Fee TrackerIf you deleted it with git rm, the deletion is staged too, so restore both places: git restore --staged --worktree README.md.
A File from an Older Commit
--source takes the file from any commit instead. Navika wants to see how the fees looked before the correction, by putting that version in her working tree:
git restore --source=HEAD~1 fees.txt
cat fees.txt
git status -s
git diffTerm 1 fee: $12,000
Term 2 fee: $12,000
M fees.txt
diff --git a/fees.txt b/fees.txt
index 6da94ac..c531a66 100644
--- a/fees.txt
+++ b/fees.txt
@@ -1,2 +1,2 @@
Term 1 fee: $12,000
-Term 2 fee: $12,500
+Term 2 fee: $12,000The file in the folder is now the version from HEAD~1, "Add the term fees". To Git, this is simply an edit, shown as M. Nothing else changed: the commits are the same, and the staging area still has the newer version. You could commit this to go back to the old fee, or restore once more to return to the newest version:
git restore fees.txt
cat fees.txtTerm 1 fee: $12,000
Term 2 fee: $12,500Any name for a commit works with --source: a short ID, HEAD~2, a branch name or a tag.
Syntax
git restore [--source=<commit>] [--staged] [--worktree] <path>...| Command | It is used to |
|---|---|
git restore <file> | Throw away unstaged edits: copy the staged version back into the working tree |
git restore --staged <file> | Unstage: copy the last commit's version into the staging area, keep the edit |
git restore -SW <file> | Do both: the file matches the last commit everywhere |
git restore --source=<commit> <file> | Put a file from any commit into the working tree |
git restore . | Throw away every unstaged edit in the current folder and below |
git restore -p <file> | Choose hunk by hunk which edits to throw away |
Older Commands You Will Still See
git restore was added in Git 2.23 (2019). Before that, the same jobs were done by commands that also do other things, and you will see them in older tutorials, answers and scripts:
| Job | Older command | Today |
|---|---|---|
| Throw away edits | git checkout -- fees.txt | git restore fees.txt |
| Unstage | git reset fees.txt or git reset HEAD fees.txt | git restore --staged fees.txt |
| File from a commit | git checkout HEAD~1 -- fees.txt | git restore --source=HEAD~1 -SW fees.txt |
They still work and do the same thing. The older git checkout form also stages the file, which is why the modern equivalent in the last row needs -SW. Prefer git restore: its name says what it does, and it can't accidentally switch your branch, which git checkout can.
Common Mistakes
Restoring a file before checking what you will lose. Run git diff <file> first. Unstaged edits that you restore are gone.
Running git restore . to undo one file. It throws away the edits in every file in the folder. Name the file.
Expecting --staged to throw away your edits. It only unstages. The file in your folder keeps the change.
Expecting git restore to undo a commit. It doesn't touch commits. To undo a commit, use git reset or git revert, which have their own lessons.
Restoring a new file that was never committed. git restore has nothing to bring back for a file that isn't in the staging area or the source commit, and reports that the path did not match any file. To remove an untracked file, delete it or use git clean.
Under the Hood
git restore is a copy from one place to another, and its options only choose the two places:
| Command | Copies from | Into |
|---|---|---|
git restore <file> | The staging area | The working tree |
git restore --staged <file> | HEAD | The staging area |
git restore -SW <file> | HEAD | Both |
git restore --source=X <file> | Commit X | The working tree |
git restore --source=X --staged <file> | Commit X | The staging area |
If the file doesn't exist in the source, restoring removes it from the target. That is why git restore --staged on a newly added file unstages it: HEAD has no such file, so it is removed from the staging area, and it becomes untracked again.
Interview Corner
How do you discard local changes to a file?
git restore <file>. It copies the staged version of the file (which is the committed version when nothing is staged) over the working tree copy. The older form is git checkout -- <file>. Unstaged changes discarded this way can't be recovered.
How do you unstage a file without losing your changes?
git restore --staged <file>. It resets the staging area entry to HEAD and leaves the working tree alone. The older form is git reset <file>.
How do you get a file back as it was in an earlier commit?
git restore --source=<commit> <file> puts that version in the working tree, where you can review it and commit it. git show <commit>:<file> only prints it.
Why was git restore introduced when git checkout already did this?
git checkout did two unrelated jobs: switching branches and restoring files. One typo could do the wrong one. Git 2.23 split it into git switch for branches and git restore for files, which are clearer and safer.
What is the difference between git restore and git reset?
git restore changes files in the working tree or the staging area and never moves a branch. git reset with a commit moves the current branch to that commit, which changes the history. On a single path, git reset <file> only unstages, like git restore --staged.
Practice
Challenge: three files, three undos. In the same repository, make these changes: add the line Run the app. to README.md and stage it, add Hostel fee: $9,000 to fees.txt without staging it, and create a new file todo.txt. Then run git restore --staged README.md and git restore fees.txt, and print the short status.
What does Git print? Before you open the solution, decide what each file's line will look like, or whether it will disappear.
Show solution
echo "Run the app." >> README.md
git add README.md
echo 'Hostel fee: $9,000' >> fees.txt
echo "call the office" > todo.txt
git restore --staged README.md
git restore fees.txt
git status -s M README.md
?? todo.txtREADME.md was unstaged but kept its edit, so it shows M. The hostel line in fees.txt was thrown away, so the file is clean and doesn't appear. todo.txt is untracked, and git restore doesn't touch untracked files.
Checkpoint. In a practice repository, make one change for each row of the syntax table above, and undo it with the matching command. Run git status -s before and after each one.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git restore <file> | Throw away unstaged edits |
git restore . | Throw away all unstaged edits in this folder and below |
git restore --staged <file> | Unstage a file |
git restore -SW <file> | Unstage and throw away edits |
git restore --source=<commit> <file> | Get a file from another commit |
git restore -p <file> | Throw away edits hunk by hunk |
Was this tutorial helpful?