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.

Author image