Sub topic 4
Unstaging files without losing edits
This chapter assumes you know git add, git commit, git status, and the three areas from the previous chapter: the working tree, the index (also called the staging area), and HEAD. Here you will remove files from the index while keeping your edits on disk.
What unstaging means
Running git add copies a file's current content into the index. Unstaging reverses that copy. The index entry goes back to matching HEAD, and the working tree is not touched. Your edits stay in the file. They are just no longer part of the next commit.
Discarding an edit and unstaging it are different operations:
| Goal | Command | Changes the index | Changes the working tree |
|---|---|---|---|
| Discard unstaged edits | `git restore notes.txt` | No | Yes |
| Unstage, keep edits | `git restore --staged notes.txt` | Yes | No |
The command
Start by looking at where each change lives:
git statusgit status lists "Changes to be committed" (the index differs from HEAD) and "Changes not staged for commit" (the working tree differs from the index). To remove a file from the first list, use the --staged option:
git restore --staged notes.txt--staged tells restore to write to the index instead of the working tree. By default it copies the version from HEAD, so the index entry matches the last commit again. Afterward, notes.txt appears under "Changes not staged for commit" with your edits intact.
Combining both flags, git restore --staged --worktree notes.txt, resets the index and the working tree together. That discards your edits, so use it only when you mean to.
A worked example
Suppose you edited two files and staged both with git add ., but you want them in separate commits.
git add .
git status
git restore --staged notes.txt
git commit -m "Update app logic"
git add notes.txt
git commit -m "Update notes"After the restore --staged line, only the other file is in the index. The first commit contains just that file. Your edits to notes.txt were never lost, so you can stage and commit them next.
Unstaging before the first commit
In a new repository there is no HEAD yet, so git restore --staged has nothing to copy from and fails. Use git rm --cached notes.txt instead. It removes the file from the index but leaves it on disk, and Git then treats it as untracked.
Older syntax
Before Git 2.23, the command was git reset HEAD notes.txt. It still works, and you will see it in older guides. git restore --staged is preferred because its name says what it does, and reset also has other jobs, covered later.
Common errors
- Confusing the two restores.
git restore notes.txtwith no flags discards unstaged edits. Those edits were never committed or staged, so Git cannot get them back. Checkgit statusbefore you run it. - Expecting a staged file to revert. If a file is staged and you then edit it again,
git restore notes.txtrestores the index version, not theHEADversion. You lose only the newest edits. - Unstaging a new file and expecting it to vanish. A newly added file becomes untracked after
restore --staged. It stays on disk. Removing it is a job forgit clean, covered in the next chapter.
Exercise
You staged app.py and notes.txt with git add ., but you only want app.py in the next commit. Which command do you run, and what happens to your edits in notes.txt?
Show answer
Run git restore --staged notes.txt. The index entry for notes.txt goes back to matching HEAD, and your edits stay in the working tree. The file moves to "Changes not staged for commit".
Exercise
You staged report.txt, then edited it again without staging. You run git restore report.txt. Which version of the file do you end up with?
Show answer
The staged version. Without --staged, restore copies from the index into the working tree, so the second round of edits is lost and the staged content stays.