How to Checkout a Remote Git Branch – How-To Geek

Linux laptop showing a bash flag
fatmawati achmad zaenuri/Shutterstock.com

If your development team uses Git, you’ll eventually need to verify someone else’s work as a branch from a remote repository. Like most branch actions in Git, switching to a remote branch is fairly simple.

Git, branches, and remotes

Git’s philosophy is to branch out often. Branches allow development to take place without altering the core code base. When you are satisfied that the new and tested code is ready, merge your new branch into another branch. Usually, this is the main or master branch, but you can combine any two branches.

Because of this flexibility, and the light and fast way Git handles branches and merges, branching was transformed. In previous version control systems, branching was a big problem. Branching and merging were slow and error-prone. Git provided developers with easy and fast branching that is used to support many different workflows.

If you work or volunteer as part of a development team using Git, you’ll have a “central” Git repository, remote from each software engineer’s computer. This is known as the remote repository, or simply the “remote” one. This is where commits and changes are sent to your local repository when you make a push.

Of course, that’s what the other developers are doing as well. This facilitates collaboration. If you need to access another developer’s work, simply retrieve your code from a branch in the remote repository. If they need to access your work, they will retrieve your code from a branch in the repository that crawls one of your local branches.

In Git, a development project can have multiple remote controls. However, a local branch can only track a single remote branch office. So, as long as you’re working with the right remote, unprotecting a remote branch with multiple remotes is the same as using a single remote.

Find your local branches

You should avoid name conflicts. If you have an on-premises branch office that has the same name as the remote branch that you are checking out, you have two options. You can rename your local branch and check out the remote branch office. This way, the local branch office that tracks the remote branch has the same name as the remote branch office. Or, you can decommission the remote branch and tell Git to create a local tracking branch with a new name.

To find out the names of the branches in your local repository, use the git branch command.

git branch

List of local branches with the command git branch

This local repository has one master branch and three other branches. The asterisk indicates which is the current branch. Moving from one branch to another requires unprotecting the branch you want to work with.

git checkout new-feature git status

Check out a local branch with the git checkout command

The first command changes the branch for us, so that “new-feature” is the current branch. The git status command verifies it for us.

We can jump back and forth between branches, commit new changes, extract updates from the remote control, and send local updates to the remote control.

RELATED: How to Update and Maintain Separate Git Branches

Checking

a Remote Branch

There is a branch in the remote repository that is not present on our machine. A developer named Mary has created a new feature. We want to switch to that remote branch so we can build that version of the software locally.

If we perform a search, Git will remove the metadata from the remote repository.

git fetch

<img src="https://www.howtogeek.com/wp-content/uploads/2023/01/3-1.png" alt="Using the git fetch command to retrieve metadata about

a remote repository” />

Because this is the first search we’ve done since Mary pushed her branch to the remote repository, we’re told there’s a new branch called “origin/mary-feature”. The default name for the first remote repository added to a project is “origin”.

Whether we see this message or not, we can always ask Git to list the branches in the remote repository.

The -r (remote) option instructs Git to report on branches that are in the remote repository.

git branch -r

Using the git branch -r command to enumerate remote branches

The point to note here is that Git is checking your local copy of the remote control’s metadata. That’s why we use the git fetch command to make sure the local copy of the metadata is up to date.

Once we detect the branch we want, we can go ahead and check it out. We use the git checkout command with the -b (branch) option, followed by the name we will use for the local branch, followed by the name of the remote branch.

git checkout -b mary-feature origin/mary-feature Decommission a remote branch with the command git checkout -b We can see that we have retired the remote branch

and created a local branch that will track changes to the remote branch.

git branch

<img src

=”https://www.howtogeek.com/wp-content/uploads/2023/01/6-1.png”

alt=”List of local branches with the git branch command, with the git command Newly created copy of the remote branch selected as the current branch” />

Our new local branch is now our current branch of work

.

Name conflict handling

If you have a local branch office that has the same name as the remote branch office, you can rename the local branch office before you check out the remote branch office or check out the remote branch office and specify a different local branch name.

To check out the remote branch in a local branch with a different name, we can use the same command we used earlier and choose a new local branch name.

git checkout -b mary-test origin/

mary-feature <img src="https://www.howtogeek.com/wp-content/uploads/2023/01/7-1.png" alt="Decommission a remote branch with the

command git checkout -b with the local branch with a different name than the remote branch” />

This creates a local branch called “mary-test” that will track local commits in that branch. The keystrokes will go to the remote branch “origin/mary-feature”.

This is probably the best way to handle local name conflicts. If you really want to keep the same local and remote branch name, you will need to change the name of your local branch before removing the remote control. Renaming a branch is trivial in Git.

git branch -m mary-feature old-mary-branch

It is now clear to retire the remote branch “origin/mary-feature”.

Handling multiple remote repositories

If you have multiple remote repositories configured, you must be careful that you are working with the appropriate repository when you decommission the remote branch.

To list remote

repositories, use the remote command with the -v (view) option. remote

git -v

<img

src=”https://www.howtogeek.com/wp-content/uploads/2023/01/9-1.png” alt=”Remote repository list with the command git remote -v

” />

To see all the available branches, we need to get the metadata of all our remote controls, then list the remote branches.

git fetch -all git branch -all

Using git fetch -all to update local metadata and using git branch -all to list all branches, local and remote

We can see that the branch we want is in the remote “source”. The command to check it is in the same format that we have already used. We need to specify the remote name, “source”, as well as the branch name, “mary-feature”.

git checkout -b mary-feature origin/mary-feature

Decommission a remote branch with the git checkout -b command, using the remote name and branch name

RELATED: How to Change, Add, and Remove Git Remotes

Before You Pay Before you pay

Before

you

pay, keep a few things in mind and you’ll be fine.

Be sure to avoid name conflicts. If you have a local branch office with the same name as the remote branch office, decide whether to rename the local branch office or create a branch office with a different name to keep track of the remote branch office.

If you use multiple remote repositories, make sure you use the correct remote control.

RELATED: Git rebase: Everything you need to know