How to Make an Existing Git Branch Track a Remote Branch?

If you want to set the upstream for the current local branch (i.e. make it track a specified remote branch), then you can use --set-upstream-to option (or its equivalent shorthand "-u") with the git branch command like so:

# git 1.8.0
git branch -u remote_name/branch_name

# or
git branch --set-upstream-to remote_name/branch_name

If you are not on the branch you want set the upstream for on your local, then you can simply append the local branch's name at the end of the command like so:

# git 1.8.0
git branch -u remote_name/branch_name local_branch_name

# or
git branch --set-upstream-to remote_name/branch_name local_branch_name

In git v1.7.x, the option name to set the upstream was --set-upstream instead of --set-upstream-to. The former option was deprecated in favor of the latter in git v1.8.0.

Please note that defining an upstream for a local branch might fail if the remote you want to set upstream to has not already been fetched locally. If that's the case, please make sure that you run the git fetch remote_name command first.


This post was published 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.