Sub topic 3
Throwing away uncommitted edits with git restore
This chapter assumes you can use git add, git commit, git status and git log, and that you know what a branch is. You should also be comfortable running commands in a terminal.
What restore does
Git tracks your work in three areas: the working tree (the files on disk), the index (the staging area for your next commit), and HEAD (the last commit on your current branch). git restore copies content from one area into another. With no options, it copies from the index into the working tree. That overwrites your edits to the file.
Discarding edits to one file
Start by checking what changed.
git status
git restore notes.txtgit status lists notes.txt under "Changes not staged for commit". After git restore notes.txt, the file on disk matches the index again. If you never staged the file, the index matches HEAD, so the file returns to its last committed content.
Staged changes are not touched
If you ran git add notes.txt and then edited the file again, git restore notes.txt only discards the edits made after the git add. The staged version stays in the index. That is useful, because it lets you keep a good staged version and drop later experiments.
The command to remove a file from the index is a different form, git restore --staged. The next chapter covers it. Use --staged for unstaging and the plain form for discarding edits.
Discarding several files at once
You can name more than one path, or use a directory or a pattern.
git restore src/app.py src/util.py
git restore src/
git restore .The last command discards every unstaged edit under the current directory. Check git status before you use it.
Common errors
error: pathspec 'x' did not match any file(s) known to git. The path is misspelled, or the file is untracked.git restoreonly works on files Git already tracks.- Nothing seems to happen. The file may have no unstaged changes. Its edits may all be staged. Run
git statusto see which section lists it. - Restoring a deleted file. If you deleted
notes.txtfrom disk without staging the deletion,git restore notes.txtbrings it back. This is a correct use of the command. - Old tutorials use
git checkout -- file. It does the same job.git restorereplaced it in Git 2.23 becausecheckoutalso switches branches, which made it easy to misuse.
Untracked files
New files that Git does not track yet are outside the reach of git restore. A separate command, git clean, removes them. A later chapter covers it.
Summary of the forms
| Command | Copies from | Copies to | Effect |
|---|---|---|---|
| `git restore file` | Index | Working tree | Discards unstaged edits |
| `git restore --staged file` | HEAD | Index | Unstages the file, keeps edits |
| `git restore --source=<commit> file` | A commit | Working tree | Replaces the file with an older version |
Only the first row is the subject of this chapter. The other two come later.
Exercise
You edited report.txt, ran git add report.txt, then made a second round of edits to the same file. You now run git restore report.txt. Which edits remain in the file, and why?
Show answer
The first round of edits remains. git restore report.txt copies the index version into the working tree, and the index holds the content you staged. Only the second round, which was never staged, is discarded.
Exercise
You deleted config.yml from disk by mistake and have not committed anything. What single command brings it back, and what does the command copy from?
Show answer
git restore config.yml. It copies the file from the index (which matches HEAD if you never staged a change) back into the working tree.