Two Ways to Back Out
git status has been showing two hints next to its lists of changes:
(use "git restore --staged <file>..." to unstage)
(use "git restore <file>..." to discard changes in working directory)
They describe two different jobs:
- Unstage: take a change out of the next commit, but keep it in your file. Nothing is lost.
- Discard: throw away an edit in your file and go back to an earlier version. The edit is gone.
Both use git restore. The difference is which version it copies, and where.
How Restore Copies Versions

Read the arrows as "copy this version over that one":
git restore <file>copies the staging area's version of the file over the one in your working tree. If you have not staged the file, the staging area still holds the last commit's version, so all your edits to it are discarded. If you staged an edit, that edit survives: only changes made after staging are discarded.git restore --staged <file>copies the last commit's version into the staging area. That unstages the file: your edit stays in the working tree, now listed under "Changes not staged for commit". A new file has no version in the last commit, so it leaves the staging area and becomes untracked again. It stays on your disk.
To discard staged and unstaged edits to a file in one step, name both places:
git restore --staged --worktree <file>
None of these commands changes a commit. They only move versions between the last commit, the staging area and your files.
What Cannot Be Brought Back
Unstaging is always safe. Discarding is not: an edit you never committed is not
stored anywhere in Git's history, so once git restore discards it, no Git
command brings it back. Your editor's undo history is the only hope.
Before discarding, look at exactly what you will lose:
git diff <file>
Every change that diff shows is what git restore <file> throws away. Take
extra care with git restore ., which discards the unstaged edits in every
file in the current folder and the folders inside it.
Committed work is different: once a version is in a commit, later lessons show several ways to get it back.
Older Commands You May See
git restore arrived in Git 2.23, which split these jobs out of the older
git checkout command. Older guides discard edits with git checkout -- <file> and unstage with git reset HEAD <file>. Both still work, but this
course uses git restore, whose name says what it does.
Exercise: Unstage One Change and Discard Another
You ran git add . and staged three changes at once: a new book for the
reading list, which you want; a line you deleted from the schedule by
accident; and a personal note that should never be committed. You will keep
the first, discard the second and unstage the third without deleting it.
Starting state: Git configured as in the configuration lesson.
-
Run the setup:
mkdir -p ~/git-practice/restore-practice cd ~/git-practice/restore-practice git init -b main cat > books.md <<'EOF' # Reading list - Paper Lanterns - A Year of Rain EOF cat > schedule.md <<'EOF' # Book club schedule Meet on the first Sunday of each month. Location: the library meeting room. EOF git add books.md schedule.md git commit -m "Start the book club notes" -
Make the three changes and stage them all at once:
cat >> books.md <<'EOF' - The Quiet Harbour EOF cat > schedule.md <<'EOF' # Book club schedule Meet on the first Sunday of each month. EOF cat > notes-to-self.txt <<'EOF' Ask Ravi about snacks. EOF git add . git statusAll three files are under "Changes to be committed":
Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: books.md new file: notes-to-self.txt modified: schedule.md -
Unstage the personal note, and check that it still exists:
git restore --staged notes-to-self.txt git status lsnotes-to-self.txtmoves to "Untracked files", andlsstill lists it. -
Unstage the schedule change:
git restore --staged schedule.md git statusschedule.mdmoves to "Changes not staged for commit". The deleted line is still missing from the file: unstaging kept your edit. -
Before discarding, check what you will lose:
git diff schedule.mdThe only change is
-Location: the library meeting room., the line you deleted by accident. Discarding the edit brings it back. -
Discard the edit:
git restore schedule.md git status cat schedule.mdschedule.mdis no longer listed, andcatshows the location line again. -
Commit the change you wanted:
git commit -m "Add The Quiet Harbour to the reading list" git statusThe commit summary says
1 file changed, 1 insertion(+).git statuslists onlynotes-to-self.txt, still untracked.
Success: the commit contains only the books.md change, schedule.md
matches the last commit, and notes-to-self.txt is still on your disk,
untracked.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
git restore <file> prints nothing and the edit is still there | The edit is staged, and restore copies the staged version, which already has it | git restore --staged <file>, then git restore <file> |
did not match any file(s) known to git | A typo, or the file is untracked: restore needs a version Git has stored | Check the name in git status; an untracked file you no longer want can simply be deleted |
| A new file moved from "Changes to be committed" to "Untracked files" | Expected after git restore --staged: the last commit has no version of it | Nothing to fix; it is still on your disk |
| Edits are gone that you meant to keep | git restore discarded them, and Git never stored them | Try your editor's undo; next time read git diff <file> first |
-
Clean up:
cd ~/git-practice ls rm -rf restore-practice ls
Independent task: before cleaning up, try two more repairs:
-
Add a line to
books.mdand stage it:cat >> books.md <<'EOF' - Small Engines EOF git add books.mdNow remove the new line completely, so
books.mdmatches the last commit. Trygit restore books.mdfirst and explain what happens. -
Delete
schedule.mdby mistake withrm schedule.md, then bring it back using Git.
Check Your Understanding
- Which of these keeps your edit in the file, and which can lose work:
git restore --staged notes.mdorgit restore notes.md? - You stage an edit to
a.txt, edita.txtagain, then rungit restore a.txt. What does the file contain afterwards, and what is still staged?
Answer and Reasoning
Independent task
-
git restore books.mdchanges nothing, because it copies the staged version, which already contains the new line. Unstage first, then discard:git restore --staged books.md git restore books.mdOr do both in one step with
git restore --staged --worktree books.md. Either way,git statusno longer listsbooks.md. -
After
rm schedule.md,git statuslistsdeleted: schedule.mdunder "Changes not staged for commit".git restore schedule.mdcopies it back from the staging area, which still holds the committed version.
Understanding check
git restore --staged notes.mdkeeps your edit: it only takes the change out of the next commit.git restore notes.mdreplaces the file with the staged version, and unstaged edits it discards are lost for good.- The file goes back to the staged version: it keeps the first edit and
loses the second. The first edit is still staged, because
git restorewithout--stagednever touches the staging area.