CodeBlanks Logo Icon CodeBlanks

git checkout

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.txt

The 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 status

After 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

terminal
# Dosyayı geri yükle

# Durumu kontrol et

Sample solution

terminal
git checkout -- notlar.txt
git status

Expected 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

Question
git checkout -- ____
Answer
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

All lessons in Beginner