set upstream branch git

How to Set Upstream Branch in Git

While working with Git, it can be very useful to learn about upstream branches, since they are commonly required to track remote branches. In this article, we will look at how to set upstream branch in Git.


What is Upstream branch in Git

Upstream branch is a remote branch that corresponds to your local remote branch. It is also known as remote tracking branch and is basically the branch tracked on repository by your local branch. Upstream branches make it easy to push and pull code to & from local branch to remote branch. It is also useful in knowing the unsynced commits between you local and remote branch.

Also read : How to Convert Callback into Promise


How to Set Upstream Branch in Git

Here are the different use cases to set upstream branch in git.


Set upstream branch with git push

Here is the syntax to create upstream branch with git push

$ git push -u <remote> <branch>

You can also use –set-upstream option to set upstream branch

$ git push --set-upstream <remote> <branch>

Let us say you have local branch named dev and want to create upstream branch origin/dev, then here is the command to do it.

$ git push -u origin dev
OR
$ git push --set-upstream origin dev

Also read : How to Write to File in Bash


Set Upstream Branch for existing local branch

If your upstream already exists, and you want to set an existing branch (e.g dev2) to link to that upstream branch, then you need to use git branch command as shown below

$ git branch -u <remote>/<branch>

Let us say you have an upstream origin/development and you want to set local branch dev2 to that upstream branch, then first checkout to dev2 branch and run the above command.

$ git checkout -b dev2
$ git branch -u origin/development

Now origin/development will be set as upstream branch of local branch dev2.

In this article, we have learnt how to set new upstream branch for local branches using git push command. We have also seen how to set an existing upstream branch for local branch using git branch command.

Also read : How to Disable HTTP Strict Transport Security Policy in Apache


Leave a Reply

Your email address will not be published. Required fields are marked *