Git Checkout
The checkout
term in Git is the maneuver of switching between different versions of a target entity. The git checkout
command is used to switch between branches or to restore working tree files.
The git checkout
command operates on three different entities, which are files, commits, and branches.
The git checkout
command allows the switch between different feature branches in one single repository.
It checks the branches and updates the working directory files to match the version of those files in the specified branch.
Checking out branches
The git checkout
command allows the navigation between the branches created with git branch command. When a branch is checked out, all the working directory files are updated to match the corresponding version of those files stored in that branch, and Git starts recording all the new coming commits on that branch.
Git has changed the traditional way of version control system workflow by allowing each feature to have a dedicated branch. This major change makes it very easy to work simultaneously on different unrelated features and try new dangerous features without the fear of destroying exiting functionalities.
Sometimes the git checkout
command can be confused with the git clone command. The difference is that the git clone
fetches data from a remote repository, whereas git checkout
works to switch code versions on the local system.
Checking out existing branches
Let us assume that your current working repository contains pre-existing branches. To switch between these branches, you can use the git checkout
. If you want to display available branches and know the current branch name, run git branch
.
$ git branch
*master
feature_wip_branch
demo_feature_branch
$ git checkout feature_wip_branch
The above example displays the list of available branches using the git branch
, and switch to the branch 'feature_wip_branch'.
Checking out new branches
The git branch
command is invoked to create a new branch. Before starting working on a new feature, you create the branch using git branch new_feature_branch
, and after that, you use git checkout new_feature_branch
to switch the branch.
Git offers a shortcut to create a new branch and switch on it using just one command. The git checkout
command has a -b
option that creates a new branch and switches on it immediately.
$ git checkout -b new_feature_branch
The above command creates a new branch named 'new_feature_branch' and checks it out directly. The -b
flag indicates a Git to execute git branch <new_feature_branch>
before running git checkout <new_feature_branch>
.
By default, the git checkout -b
will base the 'new_feature_branch' off the current HEAD
ref. Git makes it possible to base the newly created branch on a specified branch bypassing the specified branch as a parameter to the git checkout
command.
$ git checkout -b <new_feature_branch> <existing_feature_branch>
In the above command, the <new_feature_branch> will be based off the <existing_feature_branch> instead of the current HEAD
.
Switching branches
To switch branch, run the following command that will point HEAD
to the tip of <branch_name>
.
$ git checkout <branch_name>
Git tracks a history of checkout execution in the reflog. So you can run git reflog to view the history of the invocation of the git checkout
.
Checking out a remote branch
It is common to use remote repositories when collaborating with a team. Each remote repository will contain a list of branches. The first step o checkout a remote branch is to fetch the contents of the branch using git fetch.
$ git fetch -all
The modern version of Git supports to check out the remote branch like a local branch.
$ git checkout <remote_branch_name>
While in the old version of Git, to check out a remote branch, you should create a new branch based on the remote one.
$ git checkout <remote_branch_name> origin/<remote_branch_name>
In addition, you can checkout a new local branch and reset it to the remote branch's last commit using the git reset command.
$ git checkout -b <branch_name>
$ git reset --hard origin/<branch_name>
Detached HEADS
The "detached HEAD" state is used to check out commits, and inspect the repository's older state without creating a local branch. Git uses the HEAD
ref to point to the current snapshot. The git checkout
command simply updates the HEAD
ref to point to the specified commit or branch. When the HEAD
point to a branch, no problem occur, but when it points to a commit, the "detached HEAD" state happens.
The "detached HEAD" state is a warning from Git to indicate that you're acitvity is "detached" from the project's development. All the developement that are done in the "detached HEAD" state wil be lost, because there is no branch ref to point to it.

Be sure when you are working that your development take place on a branch and never on a detached HEAD. This ensure that you always have a reference to your new commits. However when you want to verify an old commit being on detached HEAD does not really matters.