git restore
- Git course
- Intermediate
- Lesson 7/82
- 4 min
Learn to restore files with git restore.
Restore a File
The git restore command returns a working tree file to a version known by Git. For example, git restore notes.txt discards changes that have not yet been staged.
This operation can cause you to lose local changes in the file. Before using it, make sure you no longer need the content you are discarding.
Undo a Change in the Staging Area
After adding a file to the staging area with git add, you may want to undo that preparation. git restore --staged removes the file from the staging area while preserving its working tree changes.
git restore --staged notes.txtThe edited file remains, but it is no longer prepared for the next commit.
Practice Unstage a Prepared File
Remove ozet.md from the staging area while preserving its working tree changes. Then print a message confirming completion.
Starter code
print("Dosya hazırlığı kontrol ediliyor")Sample solution
git restore --staged ozet.md
print("Dosya stage alanından çıkarıldı")Expected output
Dosya hazırlığı kontrol ediliyor Dosya stage alanından çıkarıldı
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which command discards file changes that have not yet been staged?
- git remote notlar.txt
- git log notlar.txt
- git restore notlar.txt Correct answer
- git branch notlar.txt
Explanation: Correct. git restore returns the file in the working tree to its recorded Git version.
2 Remove from the Staging Area
git restore ____ rapor.txt
git restore --staged rapor.txt
Explanation: The --staged option removes the file from the staging area and preserves working tree changes.
3 Restore Operations
| Term | Meaning |
|---|---|
git restore rapor.txt |
Discards a working tree change |
git restore --staged |
Removes a file from staging |
Çalışma ağacı |
The local working copy of files |
Stage alanı |
Changes prepared for a commit |