[ 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.