In Git terms, a checkout is the act of switching between different versions of a target entity. The checkout command can be used to switch the currently active branch to different branch. However, the checkout command can also be used to restore files. But the most common use of the checkout command is to switch branches.
Usage of the checkout command in different forms
- The following checkout command lets you switch currently active branch to different branch:
- Create a new branch and switch to that branch in one go.
- Create a new branch and setup tracking relationship between the local branch and the remote branch.
- Restore an old version of a file.
- Restore the last commit version of the file.
git checkout <branch-name>
Example: git checkout user_login_feature
git checkout -b <branch-new-name>
Example: git checkout -b user_login_feature
git checkout -b <branch-new-name> --track origin/user_login_feature
Example: git checkout -b user_login_feature --track origin/user_login_feature
git checkout 7a5b104 <file-name>
Example: git checkout 7a5b104 Student.java
git checkout HEAD <file-name>
Example: git checkout HEAD Student.java