Make an Existing Git Branch Track a Remote Branch?

Better Stack Team
Updated on June 24, 2024

To make an existing Git branch track a remote branch, you can use the -u or --set-upstream-to option with the git branch command or the -u or --set-upstream option with the git push command. Here's how:

Using git branch

 
git branch --set-upstream-to=<remote>/<branch> <local-branch>

Replace <remote> with the name of the remote repository, <branch> with the name of the remote branch you want to track, and <local-branch> with the name of the local branch you want to set up to track the remote branch.

For example, to make the local branch main track the remote branch main on the origin remote:

 
git branch --set-upstream-to=origin/main main

Using git push

 
git push -u <remote> <local-branch>

Replace <remote> with the name of the remote repository and <local-branch> with the name of the local branch you want to push and set up to track the remote branch.

For example, to make the local branch main track the remote branch main on the origin remote:

 
git push -u origin main

Note:

  • After setting up the tracking relationship, you can simply use git push and git pull without specifying the remote branch name, as Git will automatically use the tracked remote branch.
  • If you're already on the branch you want to set up to track a remote branch, you don't need to specify the local branch name when using git push -u.
  • Ensure that you have the necessary permissions to push changes to the remote repository and set up tracking relationships.
Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.