Git: Checkout a Remote Branch - Stack Abuse

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

Improve your dev skills!

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

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

Want a remote job?

    Prepping for an interview?

    • Improve your skills by solving one coding problem every day
    • Get the solutions the next morning via email
    • Practice on actual problems asked by top companies, like:
     
     
     

    Git Essentials

    Check out this hands-on, practical guide to learning Git, with best-practices and industry-accepted standards. Stop Googling Git commands and actually learn it!

    © 2013-2021 Stack Abuse. All rights reserved.