[ Content | View menu ]

Checking out a branch from Github

Mark Mzyk | December 7, 2011

These are the set of commands to checkout a branch of a repository from Github, which is not immediately obvious the first time you try it.

First clone the repo if you haven’t:

git clone url-to-repo-you-want

Clone has already checked out the branches, but it only initially creates the master branch. You have to tell it to create the other branches you might want.

To do that:

git checkout -t origin/branch-name

This will checkout and create a branch that has the name branch-name and that tracks the remote branch at origin/branch-name.

If you want your branch to have a different name from the remote branch:

git checkout -t -b your-branch-name origin/branch-name

This creates a branch with your-branch-name that tracks the branch at origin/branch-name.

Because these commands uses checkout you will then have the new branch as your current working branch. You can use git branch with the -t (–track) flag to create the branch without switching to it.

If you’re working in a repo you’ve already cloned and want to checkout a newer branch, it maybe necessary to do a git fetch first to make sure your local checkout knows about the remote branch before you create a local branch.

Filed in: General.

2 Comments

  1. Comment by Jed Smallwood:

    Thanks for the post. This was much clearer than the gook I ran into on stackoverflow and it had a fairly high ranking on my google search of “git checkout branch”

    January 18, 2012 @ 22:45
  2. Comment by Mark Mzyk:

    No problem. I figured if I had trouble figuring it out, others would too. Glad it helped Jed.

    January 19, 2012 @ 08:42