Git Restore

Updated · 10 min read
easyGit 2.53gitgit-restoreunstagediscard-changesinterview

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:

Terminal
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 --oneline
Output
95a4945 (HEAD -> main) Correct the term 2 fee
fe87780 Add the term fees
dd34d8a Add project README

Throw Away Your Edits

Navika starts changing fees.txt, then realizes she was editing the wrong file. She wants it back exactly as it was:

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

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

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

M (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):

Terminal
git add fees.txt
git restore --staged --worktree fees.txt
git status -s
cat fees.txt
Output
Term 1 fee: $12,000
Term 2 fee: $12,500

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

Terminal
rm README.md
git status -s
git restore README.md
git status -s
cat README.md
Output
 D README.md
Fee Tracker

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

Terminal
git restore --source=HEAD~1 fees.txt
cat fees.txt
git status -s
git diff
Output
Term 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,000

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

Terminal
git restore fees.txt
cat fees.txt
Output
Term 1 fee: $12,000
Term 2 fee: $12,500

Any name for a commit works with --source: a short ID, HEAD~2, a branch name or a tag.

Syntax

Terminal
git restore [--source=<commit>] [--staged] [--worktree] <path>...
CommandIt 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
Where git restore copies from and to

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:

JobOlder commandToday
Throw away editsgit checkout -- fees.txtgit restore fees.txt
Unstagegit reset fees.txt or git reset HEAD fees.txtgit restore --staged fees.txt
File from a commitgit checkout HEAD~1 -- fees.txtgit 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:

CommandCopies fromInto
git restore <file>The staging areaThe working tree
git restore --staged <file>HEADThe staging area
git restore -SW <file>HEADBoth
git restore --source=X <file>Commit XThe working tree
git restore --source=X --staged <file>Commit XThe 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
Terminal
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
Output
 M README.md
?? todo.txt

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

Quick check: Git restore

4 questions
1Which command throws away unstaged edits to fees.txt?
2You staged fees.txt by mistake. Which command unstages it and keeps your edit?
3Can Git bring back edits you threw away with git restore <file>?
4Which command puts the version of fees.txt from two commits ago into your folder?

Key Takeaways

Quick reference

CommandIt 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?

Lesson 23 of 106