checkout command is used to ‘check out’ of an existing branch and view another branch of code.
Checkout is essential for working on a new branch, existing branch, or remote branch.
Checkout in git operates on files, commits, and branches.
In Git version control, the git
checkout command is used to check out of an existing branch and view another branch of code. For instance, you may check out of the main branch of your code to view a branch that contains the code for a bug fix.
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.
Checkouts are an important feature of navigating through a Git repository.
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 so you can see all the files in the working tree. 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 are on the master branch and 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.
How to Checkout an Existing Branch
To checkout a branch in Git, 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:
Our code 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 could create this branch and navigate to is using this command:
git checkout -b fix-18
The above command creates the branch fix-18 then checks out the new branch. In simple terms, this command is like running git branch fix-18 then git checkout fix-18, which are used to create a new branch and checkout that branch, respectively.
By default, the branch created by the git checkout -b command will use the current HEAD branch as the template for the new branch. So, if you’re on the master branch and run the git checkout -b command, the new branch will use the same code as you have stored on the master 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.
Checkout a Remote Branch
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.
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 this code:
git fetch --all
This command fetches all changes made to the remote repository — including changes made to every branch — 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 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. This allows you to view the code on a branch, and 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!