Skip to content

Instantly share code, notes, and snippets.

@emanoelqueiroz
Forked from markSci5/Git checkout remote branch
Last active November 30, 2017 11:05
Show Gist options
  • Save emanoelqueiroz/e80b2f1b064afc1b9be991ff5d9f6584 to your computer and use it in GitHub Desktop.
Save emanoelqueiroz/e80b2f1b064afc1b9be991ff5d9f6584 to your computer and use it in GitHub Desktop.
Git checkout remote branch

With newer versions of git you can just enter:

$ git fetch
$ git checkout <branch>

git fetch will fetch all the remote branches, which you can verify with git branch -r (or git branch -rv), and as long as you don't have an existing branch with the name you want, you can just switch directly to it with git checkout <branch>. All this behavior assumes the default configuration for fetching "refs" for all remote branches and tags, which you can override with options or by configuring the remote. See git fetch --help for details. I like to also include the -p (--prune) option for removing dead remote-tracking refs for refs that have since been deleted on the remote.

To fetch a branch, you simply need to:

$ git fetch origin

This will fetch all of the remote branches for you. With the remote branches in hand, you now need to check out the branch you are interested in, giving you a local working copy:

$ git checkout -b test origin/test

Or

$ git branch test origin/test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment