The git checkout command navigates between two different branches in a Git repository. Checkout is used to view and make changes to different branches. You can check out a past commit in a repository to view how your project appeared in that state.
The git checkout command is used to check out of an existing branch and view another branch of code.
This tutorial will discuss, with examples, the basics of checking out code in Git and how to use the git checkout command. By the end of reading this guide, you’ll have all the knowledge you need to checkout code in Git like a pro.
The git checkout command lets you navigate around a Git repository. You can check out a previous commit in a repository or a branch, collectively called “refs.” You can make changes to another branch once you start viewing it using the git checkout command.
When you create a branch in Git, a new branch will be created but no changes will be made to your codebase. But if you want to start changing the code on your branch, you’ll need to check out into the branch.
As soon as you check out a branch, your working directory will be changed. This lets you see all the files in the working tree associated with that branch. Then, you can use commands like git add and git commit to push commits to the branch which you are viewing.
So, suppose you have a codebase with two branches: master and beta. If you want to start making changes to code on the beta branch, you first need to navigate to view the beta branch. That’s where the git checkout command comes in.
Note: The git checkout command is sometimes used to view old commits. However, for the purposes of this tutorial, we’ll focus on git checkouts with branches, which is the main usage of this command for beginners.
Git Checkout Branch: A Walkthough
To checkout a Git branch, you can use the git checkout command.
Before we start using the git checkout command, though, we first need to know what branches we can navigate toward. We can retrieve a list of the branches in our codebase by using the git branch command:
This command returns:
master beta-v0.9 beta-v0.8
Now we know what branches exist in our codebase, we can use the git checkout command to view one of them. Suppose we want to change our current branch to view the beta-v0.8 branch. We could do so using this code:
git checkout beta-v0.8
This command changes our current branch to beta-v0.8.
Switch to a New Branch
To switch to a new branch, you can use the git checkout command, followed by the branch to which you want to switch. The syntax for switching branches is as follows:
git checkout <branch>
Suppose you want to switch to viewing the fix-19 branch on your code. You could do so using this code:
git checkout fix-19
This command changes our HEAD to the fix-19 branch, allowing us to view and manipulate the code stored in the fix-19 branch.
How to Checkout a New Branch
The git checkout command is commonly used with the git branch command. First, you can use the git branch command to create a new branch. Then, you can use git checkout to start pushing commits to the new branch you have created.
However, there is a way in which you can create a new branch then checkout into the new branch. That’s where the checkout -b flag comes in.
Suppose we want to create a new branch called fix-18 and immediately checkout into that branch. This branch will contain the code that fixes issue #18 in our hypothetical repository. We can use the following command to create a new local branch that matches the version of our HEAD branch:
git checkout -b fix-18
The above command creates the branch fix-18 then checks out the new branch. Our HEAD has switched to create a new branch.
In simple terms, this command is like running git branch fix-18 then git checkout fix-18. These commands are used to create a new branch and checkout that branch, respectively.
The branch created by the git checkout -b command uses the current Git HEAD branch as the template for the new branch.
There is a way to override this, however. The syntax for creating a new branch based on a specified existing branch, then checking out to that branch, is as follows:
git checkout -b <new-branch> <template-branch>
Suppose we want to create a new branch called fix-19 and use the beta-v0.9 branch as our template for the new branch. We then want to checkout into our new branch. We could do so using this code:
git checkout -b fix-19 beta-v0.9
This command creates the fix-19 branch which is based on the code in our beta-v0.9 branch. So, instead of being based on the current HEAD branch, we have specified the branch upon which our new branch should be based.
Git Checkout Remote Branch
You can check out a remote branch using the git fetch –all command and then the git checkout command. A remote branch is a branch stored on the repository from which you fetch code.
On team projects, you will likely be using repositories whose main version is stored on a remote server. Then, each developer on the team will have their own local branch of the code.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
If you want to check out a remote branch, there are a few steps you’ll need to follow. First, you need to fetch the contents of the remote branch to your local repo. You can do so using the git fetch command:
git fetch --all
This command fetches all changes made to the remote repository and includes them in your local version of the repo. Then, you can check out the remote branch as you would with any other branch, by using this syntax:
git checkout <branch>
The above command will allow you to checkout the remote branch on your local machine. This is because your local machine gathered a copy of all the remote branches when you run git fetch –all.
The git checkout command allows you to switch between branches in a codebase. Once you have checked out a branch you can use commands like git add and git commit to push changes to the branch.
This tutorial discussed, with reference to examples, the basics of checking out branches in Git and how to use the git checkout command. Now you have the tools you need to start using the git checkout command like a professional engineer!
To learn more about coding with Git, read our How to Learn Git guide.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.