Summary - Undoing changes
- Git course
- Beginner
- Lesson 52/80
- 5 min
Combine checkout and restore to undo changes.
Combine restoration methods
Git restoration methods depend on where the changes are located. You can discard working tree changes with git checkout -- or git restore, and unstage changes with git restore --staged.
git restore notes.txt
git restore --staged report.txt
git statusThese commands do not delete the latest commit; they only change the working tree or staging state of the specified files.
Practice Fix the state of two files
Discard the working tree changes in page.html. Unstage style.css while preserving its working tree changes. Finally, run git status.
Starter code
# Çalışma alanındaki değişikliği geri al
# Diğer dosyayı sahneden çıkar
# Durumu kontrol etSample solution
git restore sayfa.html
git restore --staged stil.css
git statusExpected output
On branch main Changes not staged for commit: modified: stil.css
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which command unstages changes while preserving them?
- git restore file.txt
- git restore --staged file.txt Correct answer
- git rm file.txt
- git checkout -- file.txt
Explanation: <code>git restore --staged</code> unstages the file while preserving its working tree changes.
2 Restore the working tree
git ____ page.html
git restore page.html
Explanation: <code>git restore page.html</code> discards the file's working tree changes.
3 Match restoration commands
| Term | Meaning |
|---|---|
git checkout -- a.txt |
Restores working tree |
git restore b.txt |
Modern restoration |
git restore --staged c.txt |
Unstages a file |
HEAD |
Identifies latest commit |