Downloading a new remote branch in Git

The most easiest way to download a new branch from remote repository is :

# Download a new remote branch named topic and switch to topic
$ git checkout topic

What above command do includes :

  1. Fetch topic branch from remote

  2. Set up its upstream branch :

    Here topic is tracking <remote>/topic. Thus when you execute git pull, Git will know where to get update.

  3. Switch to topic branch

Note: $ git checkout --track origin/topic does the same thing with $ git checkout topic .

--track sets up which remote branch the new branch is tracking.

Here -b is ignored, meaning the new branch is derived from the remote branch.

git chekcout <remote-branch-name> a shortcut, if you want to give a different branch name with the remote one, use the full format :

# Create a new branch from a remote branch and switch to it.
$ git checkout -b <branch> <remote>/<branch>

# Examples:
# Create a new branch setupfix from origin/issue13 and switch to setupfix.
$ git checkout -b setupfix origin/issue13