git checkout
- Git course
- Beginner
- Lesson 49/80
- 4 min
Learn to restore a file with git checkout.
Restore a file with git checkout
The git checkout -- file.txt command restores a working tree file to its last committed state. The -- separator indicates that the following name is a file, not a branch. This operation discards uncommitted changes in that file.
git checkout -- notes.txtThe default source for restoration is HEAD, the latest commit at the current position.
Restoring from HEAD
HEAD identifies the latest commit at the current position in a Git repository. If a file has uncommitted changes, git checkout -- removes those changes and restores the file content from HEAD.
git status
git checkout -- index.html
git statusAfter restoration, the file's uncommitted changes are no longer present. Check whether you need the current content before running the command.
Practice Restore a file to its previous state
Write the command to restore notes.txt to its state in the current HEAD commit. Then check the working tree status with git status.
Starter code
# Dosyayı geri yükle
# Durumu kontrol etSample solution
git checkout -- notlar.txt
git statusExpected output
On branch main nothing to commit, working tree clean
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which version does the command restore?
- The version of all most recently modified files
- An empty file
- The version in the latest commit Correct answer
- The version when the file was first created
Explanation: <code>git checkout --</code> restores the specified file to its state in the current <code>HEAD</code> commit.
2 Restore the file to the HEAD version
git checkout -- ____
git checkout -- rapor.txt
Explanation: <code>git checkout -- rapor.txt</code> restores the file to its latest committed state.
3 Checkout concepts
| Term | Meaning |
|---|---|
HEAD |
Current latest commit |
-- |
File separator |
git checkout -- a.txt |
Restores a file |
Kaydedilmemiş değişiklik |
Uncommitted content |