Summary - Checkout and switch
- Git course
- Intermediate
- Lesson 8/82
- 5 min
Combine checkout, switch and restore to manage branch changes.
Separate Branch and File Operations
In Git, switching branches and restoring files are different operations. In modern usage, git switch is preferred for changing branches, while git restore restores file contents. git checkout is the older multi-purpose command used for both tasks.
git switch surum
git restore aciklama.txtUsing these commands for their intended purposes makes your workflow clearer.
Practice Switch Branch and Restore a File
Switch to the release branch. Then restore description.txt in the working tree and print a completion message.
Starter code
print("Dal ve dosya işlemleri başlıyor")Sample solution
git switch surum
git restore aciklama.txt
print("Dal değişti ve dosya geri yüklendi")Expected output
Dal ve dosya işlemleri başlıyor Dal değişti ve dosya geri yüklendi
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which modern pair of commands is appropriate for restoring a file and switching to another branch?
- git remote and git push
- git restore and git switch Correct answer
- git branch and git merge
- git status and git log
Explanation: Correct. git restore restores a file, while git switch changes branches.
2 Use the Revision Pointed to by HEAD
git restore --source=____ aciklama.txt
git restore --source=HEAD aciklama.txt
Explanation: HEAD is a reference to the current position. The --source=HEAD option uses the file content at that position as the source.
3 Branch and File Management
| Term | Meaning |
|---|---|
git checkout |
Operates on a branch or file |
git switch |
Changes the active branch |
git restore |
Restores file content |
HEAD |
Points to the current revision |