How to create a local copy of a remote branch in Visual Studio – 116

Suppose you’ve cloned a project from a GitHub repo that contains multiple branches. By default, Git pulls down the main tracking branch (e.g. master in most cases). That’s why you see a local checked-out (bolded) master and a remotes/origin master branch.

For the other branches under remotes/origin, Git doesn’t automatically pull these down. The branches listed under remotes/origin are called “remote tracking branches”. I believe these are also referred to as “remote branches” but the “tracking” is implied. Can you have a remote branch that isn’t a remote tracking branch? I guess not. I guess even if you checked out a “remote tracking branch” without the tracking flag (or without the checkbox checked), you’d still have the original “remote tracking branch” alongside the local non-tracked branch.

Team Explorer - Branches showing remote tracking branches

You cannot check out these remote branches directly. I guess if you could, you’d be working directly on the server instead of your local machine, which wouldn’t make too much sense, and probably wouldn’t be possible since a bare git repo (aka the git server you’re checking out from) doesn’t have a concept of a working directory. No idea. Just thinking out loud here.

Suppose you want to checkout the `for-review` branch to do work. First you’d need to create a new branch. Note just like 13* we’re going the long way around, meaning we’re not going to choose the “checkout” command in the context menu. We’re going to manually create the local branch first, then checkout, to see the process.

First, right click on the remote branch, and select New Local Branch From on the context menu, just as if you were creating any other branch. The Create Branch window section will appear.

creating a local branch from a remote tracking branch

Note how Visual Studio auto-populates the name of the local branch as `for-review` You could change the local branch name. You could also do `private string @long`. I’m sure the `@long` is far worse.

Checking the Create Branch button creates the local `for review` branch. Because the default is the Checkout branch option checked, the `for-review` is bolded.

for-review local branch checked out

I believe the Checkout command on the context menu for a remote branch will perform the above steps, but I’m not sure if there are any differences. For example, I’d assume the checkout command keeps the defaults (e.g. Track remote branch and Checkout branch – of course). In any case, when I’m trying to learn something new, it is helpful for me to see what is going on step-by-step as much as possible.

If I have any of the above terminology wrong, please let me know!!

*Heaven Sent was one of the best Doctor Who episodes in a LONG time. Wow, just wow.

3 thoughts on “How to create a local copy of a remote branch in Visual Studio – 116

  1. Technically, you can checkout remote branches. After you do a fetch, the commits are in your local repo. Remote branches are still references (pointers) to a commit, so you can do the checkout but will be in a detached HEAD. This allows you to examine/compile/run the code. You can even do commits, but being in a detached HEAD, you’ll lose those changes if you don’t create a branch or tag before doing a checkout to another branch/ref.

    It’s a common misunderstanding here at work – the other devs think when they look at remotes/origin in VS that VS connects to the remote server and shows a list of branches on the server. I might say “hey Bob, I just pushed up a new branch, check it out” and Bob opens the solution and goes to remotes/origin and doesn’t see the new branch.

    Same with “git branch -a”. They are listing what you already have in your local repo. When you do “git fetch origin”, all new commits and refs are downloaded from the server. Those refs include new branches on the server. So I tell Bob to do a fetch first, then he’ll see my new branch.

    However, it doesn’t remove refs (i.e. branches deleted on the server). You need to do “git remote prune origin” to remove your local copies of remote branches (just the refs, not the commits) that are no longer on the remote server.

    You can use “git remote show origin” to see what’s on the server.

    Liked by 1 person

    1. Thanks Jeff! You’ve answered something like 7 questions I had all in a single comment!

      > Remote branches are still references (pointers) to a commit, so you can do the checkout but will be in a detached HEAD.

      Ahhh gotcha. I’m getting closer to needing to know how the underlying plumbing works because I feel like I’m learning rules to a game where everything can be arbitrary, instead of being able to say “ahh, that makes sense because of how git works.” One day at a time!

      Thanks again for the additional tip materials!

      Like

Leave a comment