Unstage
- Git course
- Beginner
- Lesson 51/80
- 4 min
Learn to unstage a file that was added.
Unstage a staged file
After adding a file with git add, you may want to undo that operation. The git restore --staged file.txt command removes the file from the staging area while preserving its working tree changes. You can then edit the file again or stage it later.
git add report.txt
git restore --staged report.txt
git statusThis operation does not delete the file content; it only changes its staging state.
Unstage and reset
Another way to unstage changes is the git reset HEAD file.txt command. This usage reverses the file's staging entry while preserving its working tree edits. The modern and explicit alternative is git restore --staged.
git add notes.txt
git reset HEAD notes.txt
git statusThe goal here is not to delete the latest commit, but only to change the file's staging state.
Practice Undo staging
The content.html file was previously added to the staging area with git add. Unstage the file, preserve its working tree changes, and then check the status.
Starter code
# Dosyayı sahneden çıkar
# Durumu kontrol etSample solution
git restore --staged icerik.html
git statusExpected output
On branch main Changes not staged for commit: modified: icerik.html
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 How do you preserve changes while unstaging a file?
- git restore --staged file.txt Correct answer
- git restore file.txt
- git rm file.txt
- git checkout -- file.txt
Explanation: The <code>--staged</code> option unstages the file while preserving its working tree changes.
2 Unstage a file
git restore ____
git restore --staged
Explanation: The <code>--staged</code> option reverses the file's staging state and preserves its working tree content.
3 Unstage concepts
| Term | Meaning |
|---|---|
Staging area |
Area before a commit |
--staged |
Unstaging option |
git reset HEAD |
Older unstaging method |
Working tree |
Area where edits are kept |