How to checkout a remote branch for the first time via the command line – 123

In Tip 116 you saw you to create a local copy of a remote branch in VS. In today’s tip, you’ll learn how to do this from the command line.

First, let’s verify our current list of branches.

git branch -a showing only 1 local branch 

Next, you can use the following git command (provided you only have one remote)

$ git checkout I-am-a-new-branch

It seems that you don’t have to specify the `origin/` part. Git knows to look for corresponding tracking branches.

And you’ll have your local branch.

creating a new local branch from a remote tracking branch

As I’ve mentioned time and time again, I hate shortcuts when learning things for first time. This SO answer tells me there’s a more complete way.

$ git checkout -b <branch-name> origin/<branch-name>

I gave this a try with my gh-pages branch and sure enough it worked!

git checkout -b gh-pages origin/gh-pages

2 thoughts on “How to checkout a remote branch for the first time via the command line – 123

  1. It might be interesting to note that “upstream branch” is the same thing and your tip 121 shows which remote branch is being tracked. People shouldn’t assume that a local branch that happens to have the same name as a remote branch is tracking that remote branch. It might not be tracking any remote branch or it might be tracking a remote branch that has a different name.

    You could have done “git checkout -b I-am-a-new-branch master” or “git checkout -b foo origin/I-am-a-new-branch”.

    A user might do “git branch I-am-a-new-branch” and it won’t be tracking the remote. This can be fixed with “git branch -u origin/I-am-a-new-branch” (if on I-am-a-new-branch) or “git branch -u origin/I-am-a-new-branch I-am-a-new-branch” (if on a different branch).

    Like

Leave a comment