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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github