How to Checkout a Remote Git Branch?

Find out how to checkout a remote branch in Git

Depending on whether you're working with a single remote (such as origin), or multiple remotes, you have the following options to checkout a remote git branch:

Single Remote

If you have a single remote, you can simply use the following commands to checkout a remote branch:

# git 1.6.6+
git fetch
git checkout branch_name

This will create the branch locally if it doesn't exist already and set the upstream to the remote-tracking branch "origin/branch_name".

Please note that this is different from git checkout remote_name/branch_name. Using that command would result in a detached HEAD (unnamed branch).

You may, alternatively, also use the following commands (especially when you want to name the local branch differently):

git fetch remote_name
git checkout -b local_branch_name --track remote_name/remote_branch_name

Multiple Remotes

If you have multiple remotes, then you can use the following commands to checkout a specific remote branch:

git fetch remote_name
git checkout -b local_branch_name remote_name/remote_branch_name

This will create the local branch called "local_branch_name" and set the upstream to the remote-tracking branch "remote_name/remote_branch_name".

In older versions of git, you may have to add the --track (or -t) option as well, for example, like so:

git fetch remote_name
git checkout -b local_branch_name --track remote_name/remote_branch_name

Starting Git v1.6.1+, if your local branch and remote branch names are the same, then you may use the following shortcut:

# git 1.6.1+
git fetch remote_name
git checkout --track remote_name/remote_branch_name

You can also use the shorthand flag -t for the --track option for an even shorter syntax:

# git 1.6.1+
git fetch remote_name
git checkout -t remote_name/branch_name

Hope you found this post useful. It was published . Please show your love and support by sharing this post.