Skip to content
Related Articles

Related Articles

How to Set Upstream Branch on Git?

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 20 Feb, 2022

When you want to clone a new repository or to work with various feature branches, you need to know how to work with upstream branches and how you can set them up. This article will tell you how to setup upstream branches and it will also tell you which git local branch is tracking which upstream remote branch.

Prerequisites:

  • Git installed and configured on your local machine.
  • A cloned Git repository or your own Git project exists locally

What is Git Upstream Branch?

When you want to checkout a branch in git from a remote repository such as GitHub or Bitbucket, the “Upstream Branch” is the remote branch hosted on Github or Bitbucket. It’s the branch you fetch/pull from whenever you issue a plain git fetch/git pull basically without arguments.

How to Set Upstream Branches in Git

Using the Git Push command with the “-u” option for the upstream branch.

Set Upstream Branch using Git Push command

Create a new branch with the name ” and switch to the current branch using the -b option

git checkout -b <branch name>

Switching the branch confirmation appears below:

Switching the branch confirmation 

When the current branch i.e (‘new_branch’) has no Upstream branch set and we try to run the command “Git push”. After running the below command in cmd:

Now, you need to set the upstream branch using the Git push command with the -u option.  Replace <branch name> with your branch name.

git push -u origin <branch name>

 Alternatively, you can use the ‘–set-upstream’ command as well to set the Upstream branch

git push --set-upstream origin <branch name>

How to Change Upstream Branches in Git

Now, you need to track a new upstream branch than the one you just setup running:

git branch -u <remote/branch name>

For example:

git branch main -u <origin/new_branch>
git branch main -u <origin/main>

The terminal prints out the confirmation message:

How to check which Git Branches are tracking which Upstream Branches

Now, you can list all your branches that are tracking upstream branches using “Git branch” with the -vv option:

git branch -vv

The main branch has a tracking branch of [origin/main]. The test branch has a tracking branch of [origin/test]. The new_branch branch has a tracking branch of [origin/new_branch].

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!