The git checkout command navigates between two different branches in a Git repository. Checkout is used to view and make changes at different locations. You can check a previous commit in a repository to see how your project appeared in that state.
The git checkout command
is used to check out an existing branch and view another code branch
.
This tutorial will discuss, with examples, the basics of code extraction 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 review code in Git like a pro.
Git Checkout
The git checkout command allows you to browse a Git repository. You can check out a previous commit to a repository or branch, collectively referred to as “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 is created, but no changes are made to the code base. But if you want to start changing the code in your branch, you’ll need to sign up for the branch.
As soon as you retire a branch, your working directory will change. This allows you to see all the files in the work tree associated with that branch. You can then use commands like git add and git commit to send commits to the branch you’re viewing.
So, let’s say you have a codebase with two branches: master and beta. If you want to start making code changes in the beta branch, you must first 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 will focus on git checks with branches, which is the main use of this command for beginners.
Git checkout branch
: A tutorial
To check out a Git branch, you can use
the git checkout command.
However, before we start using the git checkout command, we first need to know which branches we can navigate towards. We can retrieve a list of
the branches in our codebase using the git branch command:
This command returns:
Now that we know which 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 see the beta-v0.8 branch. We could do it using this code:
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 you want to switch to. The syntax for changing branches is as follows:
Suppose you want to switch to the fix-19 branch display in your code. You can do this using this code:
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 check out 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. You can then use git checkout to start sending commits to the new branch you’ve created.
However, there is a way in which you can create a new branch and then check out in 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 on 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:
The above command creates the fix-18 branch and then retires the new branch. Our HEAD has changed to create a new branch.
In simple terms, this command is like running git branch fix-18 and then git checkout fix-18. These commands are used to create a new branch and retire that branch, respectively.
The branch created by the git checkout -b command uses the current Git HEAD branch as a template for the new branch
.
However, there is a way to override this. The syntax for creating a new branch based on a specified existing branch and then deleting that branch is as follows:
Suppose we want to create a new branch named fix-19 and use the beta-v0.9 branch as our template for the new branch. Then we want to pay at our new branch. We could do it using this code:
This command creates the fix-19 branch that is based on the code of our beta-v0.9 branch. Therefore, instead of relying on the current HEAD branch, we have specified the branch on which our new branch should be based.
Git Checkout
remote branch
You can check out a remote branch by using the git fetch -all command and then the git checkout command. A remote branch is a branch stored in the repository from which you get your code.
In team projects, you might use repositories whose major version is stored on a remote server. Then, each developer on the team will have their own local branch of code.
If you want to withdraw a remote branch, there are a few steps you will need to follow. First, you need to get the contents from the remote branch office to your local repository. You can do this using the git fetch command:
This command retrieves all changes made to the remote repository and includes them in your local version of the repository. You can then check out the remote branch as you would any other branch, using this syntax:
The above command will allow you to check out 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 conclusion
The git checkout command allows you to switch between branches in a code base. Once you have checked out a branch, you can use commands such as git add and git commit to push changes to the branch.
This tutorial discusses, 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.