Skip to main content

Git Checkout

In Git, the term checkout is used for the act of switching between different versions of a target entity. The git checkout command is used to switch between branches in a repository.

The git checkout command operates upon three different entities which are files, commits, and branches. Sometimes this command can be dangerous because there is no undo option available on this command.

It checks the branches and updates the files in the working directory to match the version already available in that branch, and it forwards the updates to Git to save all new commit in that branch.

Git Checkout Syntax:

git checkout <branchname>

Git Checkout Common Usage


  • git checkout <branchname> : To switch between branches in a repository

    Example:

    git checkout dev

    Output:

    Switched to branch 'dev'


  • git checkout -b <branchname> : The git checkout -b option is a convenience flag that performs run git branch <new-branch> operation before running git checkout <new-branch>. You can not only create a new branch but also switch it simultaneously by a single command.

    Example:

    git checkout -b dev2

    Output:

    Switched to a new branch 'dev2'


  • git checkout <remotebranch> : In the latest versions of Git, you can check out the remote branch like a local branch. It is a way for a programmer to access the work of a colleague or collaborator for review and collaboration.

    Example:

    git checkout edited

    Output:

    Switched to a new branch 'edited'
    Branch 'edited' set up to track remote branch 'edited' from 'origin'.

    edited is my remote branch. Here, we have switched to edited branch from master branch by git command line.


You can learn more about the git checkout command and its options in git-scm's documentation.