CodeBlanks Logo Icon CodeBlanks

git checkout

Learn to switch branches and files with git checkout.

Switch Branches with git checkout

git checkout is an older, multi-purpose Git command used to switch to another branch. You can move to a branch by writing its name after the command.

git checkout test-environment

This command can also be used to inspect an earlier commit. However, using separate commands for restoring files and switching branches is clearer.

Restore Files with Checkout

In its older usage, git checkout can also restore files to the content stored in a specific revision. For example, git checkout HEAD -- index.html restores index.html to the version recorded by HEAD.

This operation can discard current working tree changes in the file. In modern Git usage, git restore is clearer for restoring files.

Practice Switch to the Review Branch with Checkout

Use git checkout to switch to the branch named inceleme. Then print a message indicating that the switch is complete.

Starter code

terminal
print("Dal değişimi hazırlanıyor")

Sample solution

terminal
git checkout inceleme
print("İnceleme dalına geçildi")

Expected output

Dal değişimi hazırlanıyor
İnceleme dalına geçildi

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which command switches to the test-environment branch in the current repository?

  • git checkout test-ortami Correct answer
  • git checkout -d test-ortami
  • git restore test-ortami
  • git commit test-ortami

Explanation: Correct. Using git checkout followed by a branch name switches to that branch.

2 Select a Branch with Checkout

Question
git checkout ____
Answer
git checkout inceleme

Explanation: The branch name follows the command. This switches to the inceleme branch.

3 Checkout Uses

Term Meaning
git checkout inceleme Switches to the named branch
git checkout HEAD -- dosya Restores a file to HEAD content
HEAD Identifies the current revision
-- Separates the path from options

All lessons in Intermediate

20 Intermediate final

  1. Branch management
  2. Rebase
  3. Conflict
  4. PR flow