git checkout

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

  1. The following checkout command lets you switch currently active branch to different branch:
  2. Syntax
    
        git checkout <branch-name>
    

    Example: git checkout user_login_feature

  3. Create a new branch and switch to that branch in one go.
  4. Syntax
    
        git checkout -b <branch-new-name>
    

    Example: git checkout -b user_login_feature

  5. Create a new branch and setup tracking relationship between the local branch and the remote branch.
  6. Syntax
    
        git checkout -b <branch-new-name> --track origin/user_login_feature
    

    Example: git checkout -b user_login_feature --track origin/user_login_feature

  7. Restore an old version of a file.
  8. Syntax
    
        git checkout 7a5b104 <file-name>
    

    Example: git checkout 7a5b104 Student.java

  9. Restore the last commit version of the file.
  10. Syntax
    
        git checkout HEAD <file-name>
    

    Example: git checkout HEAD Student.java