Checking out a new remote branch in Git

If you have just one remote Git repository, the easiest way to check out a new branch from the remote is:

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

What above command does includes :

  1. Fetch “topic” branch from remote

  2. Set up its upstream branch :

    Here the branch “topic” is tracking /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 if you just have one remote named origin.

--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 a shortcut. If you have multiple remotes or you want to give a different branch name with the remote one, use the full format which specifies the remote:

# 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

Leave a Reply