Git: Checkout a Remote Branch

In order to checkout a branch from a remote repository, you will have to perform two steps. First, you need to fetch the actual branch data, which includes the commits, files, references, etc. Second, you'll want to actually check it out so your working directory contains the branch files.

This can be done fairly simply in Git. The following commands assume you only have one remote repo for your repository:

$ git fetch
$ git checkout <branch>

Using git fetch without any parameters like this will retrieve all branches from the remote repo, but if you have multiple remote repos then you should specify which one to retrieve from:

$ git fetch <remote-repo>

But of course, fetch is only needed if you haven't retrieved updates from the remote recently. If you have, then you can simply use one of the checkout commands detailed here.

Then to checkout the branch you want, and to tell Git to track it to the remote branch via the -t argument, use the following command:

$ git checkout -t <remote-repo>/<remote-branch>

Not specifying a local branch name will use the same name as the remote branch. Of course, you can also specify a different local branch name with like this:

$ git checkout -b <local-branch> <remote-repo>/<remote-branch>

There are quite a few ways to do the same thing in Git, but in some cases it depends on what version of Git you're currently using. For example, the first set of commands shown in this article are not available in Git versions < 1.6.6. So be mindful of this in case it doesn't work for you.

Last Updated: June 11th, 2019
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Free
Course

Git Essentials: Developer's Guide to Git

# git

Git Essentials: Developer's Guide to Git is a course for all developers, beginner to advanced, and written to get you up to speed with the...

David Landup
François Dupire
Jovana Ninkovic
Details

Make Clarity from Data - Quickly Learn Data Visualization with Python

Learn the landscape of Data Visualization tools in Python - work with Seaborn, Plotly, and Bokeh, and excel in Matplotlib!

From simple plot types to ridge plots, surface plots and spectrograms - understand your data and learn to draw conclusions from it.

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms