Definition

The git checkout command switches branches or restores working tree files. It operates on files, commits, and branches. The git checkout command allows switching between multiple features in just a single repository.

Checking out branches

The git checkout command works with the git branch command. It updates the files in the working directory to match the version stored in that branch telling Git to record all the new commits. The git checkout command can be associated with git clone. The git clone command operates to fetch code from a remote repository whereas git checkout operates to switch code versions on the local system.

Checking out existing branches

Consider there is a repository that contains pre-existing branches. If you want to switch between these branches you can invoke git checkout. Executing git branch will display the list of available branches and the current branch name.

git branch
master
test_branch
feature_branch
git checkout feature_branch

Checking out new branches

The git branch command is executed to create a new branch. However, git checkout -b argument creates a new branch and directly switch to it:

git checkout -b <new_branch>

In the given example, the -b flag tells Git to execute git branch <new_branch> before invoking git checkout <new_branch>.

git checkout -b <new_branch> <existing_branch>

The git checkout -b bases the new_branch off the current HEAD. In the given example, <existing_branch> will base new_branch off instead of the current HEAD.

Switching branches

Invoking the following will point HEAD to the tip of <branchname>:

git checkout <branchname>

The git reflog command can be executed to view the history. Read more about it on our git reflog.

Checking out remote branches

It is common to use remote repositories when working with a team. Each remote repository can contain its own branches. So as to checkout a remote branch git fetch should be executed to fetch the contents of it:

git fetch --all

The modern versions of Git allow checking out the remote branch like a local branch:

git checkout <remote_branch>

While in the older versions of Git, you should create a new branch based on the remote one:

git checkout <remote_branch> origin/<remote_branch>

Then checkout a new local branch and reset it to the remote branches last commit with the git reset command:

git checkout -b <branchname>
git reset --hard origin/<branchname>

Detached HEADS

“Detached HEAD” state allows you to check out commits and examine the repository’s older state without creating a local branch. The HEAD command updates the git checkout to point the specified branch or commit. No problem will occur, when HEAD points to a branch, but when it points to a commit, it moves to a “detached HEAD” state.

“Detached HEAD” state is a warning which informs that your activity is “detached” from the project’s development. Developing a feature in a “detached HEAD” state, no branch will allow you to get back to it. When checking out another branch you cannot reference your feature:

git checkout

The development must take place on a branch not being on “detached HEAD” thus ensuring to have a reference to the new commits. But when you want to look at an old commit being on “detached HEAD” state does not really matters.

Practice Your Knowledge

What are the functionalities and features of the 'git checkout' command?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.

Do you find this helpful?