How to Checkout a Remote Git Branch
This article outlines the steps necessary to checkout a remote Git branch that does not exist in the local repository.
When collaborating as a team on a project, a developer may create a new branch and push it to the remote repository. Other developers will have to download the branch from the remote for it to be accessible locally.
So, how do you checkout such a branch?
Checkout a Remote Git Branch With One Remote
Assuming we are collaborating on a project as a team and one developer creates a test branch and pushes it to the remote, how do we locally checkout the test branch?
First, we will need to fetch from the remote repository. We will run:
$ git fetch
This will bring down all changes from the remote repository to our local machine. We can see all the branches available for checkout with the git branch
command, as illustrated below.
$ git branch -v -a
This will give you all the remote and local branches available in your repository. To start working on the Test
branch, we will use the git checkout
command, as illustrated below.
$ git checkout Test
By default, Git will switch to the Test
branch and set it up to track the Test
branch from our remote.
Checkout a Remote Git Branch With Multiple Remotes
Again, we will start by fetching from the remote. However, this time, we will specify a remote.
$ git fetch origin
We will then use the git checkout
command to create a local branch based on the remote branch.
$ git checkout Test
To checkout a remote branch that does not exist in your local repository, you will need to fetch from the remote first.
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedIn