How to Checkout a Remote Git Branch?

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

Single Remote

In Git 2.23+, you can simply use the git switch command, which will create a new local branch based on the remote branch of the same name (if it does not exist already) and set up a tracking relationship:

# git 2.23+
git fetch
git switch remote_branch_name

Similarly, in Git 1.6.6+, if you have a single remote, you can use the git checkout command to check out a remote branch, like so:

# git 1.6.6+
git fetch
git checkout remote_branch_name

These commands will create the branch locally if it doesn't exist already and set the upstream to the remote-tracking branch "origin/remote_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 either set of the following commands (especially when you want to name the local branch differently):

git fetch remote_name
git switch -c local_branch_name --track remote_name/remote_branch_name
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 either set of the following commands to check out a specific remote branch:

# git 2.23+
git fetch remote_name
git switch -c local_branch_name remote_name/remote_branch_name
git fetch remote_name
git checkout -b local_branch_name remote_name/remote_branch_name

Please note that in older versions of git, you may have to explicitly add the --track (or -t) option as well.

Either of the commands mentioned above will create the local branch called "local_branch_name" and set the upstream to the remote-tracking branch "remote_name/remote_branch_name".

If your local branch and remote branch names are the same, then you may use either of the following shortcuts:

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

You can also use the shorthand flag -t in place of the --track option for an even shorter syntax.


This post was published (and was last revised ) by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.