Git

Set Upstream Branch in Git

When a new feature is added to any git repository or the user wants to clone a git repository through a branch, then the upstream branch is used. The git user can select the location of the local branch and modify the default remote branch by setting the git upstream branch. Normally, the names of the local branch and remote branch are kept the same by the git user. After updating the local branch locally, the changes made are pushed to the remote branch. Sometimes, it is required to push the modified content of the local branch to the remote branch. These tasks can be done using the –set-upstream option of the git command. How this git option can be used in different ways has been shown in this tutorial.

Pre-requisites:

Install GitHub Desktop

GitHub Desktop helps the git user to perform the git-related tasks graphically. You can easily download the installer of the latest version of this application for Ubuntu from github.com. You have to install and configure this application after download in order to use it. You can also check the tutorial for installing GitHub Desktop on Ubuntu to know the installation process properly.

Create a GitHub Account

You will be required to create a GitHub account to publish any local repository.

Create a Repository

You have to create a local repository and publish the repository in the remote server to check the commands used in this tutorial.

Initialize the git Repository

Go to the local repository folder from the terminal and run the following command to initialize the local repository.

$ git init

Set Upstream Branch Using Push:

Any new branch of the local repository can be pushed to the remote server by using the –set-upstream option or -u option. The uses of these options have been shown in this part of the tutorial.

A. Upstream branch using –set-upstream option

Run the following commands to check the branch list of the current repository and create a new branch, named secondary using the -b option.

$ git branch
$ git checkout -b secondary
$ git branch

The following output shows that there was only one branch named main in the current repository. A new branch named secondary has been created by using the -b option.

Run the following command to push the new branch of the local repository to the remote repository that is published on github.com. You have to authenticate the GitHub user account to push the newly created branch into the remote server.

$ git push --set-upstream origin secondary

The following output will appear if the GitHub account is authenticated properly.

You can check the remote repository from github.com to verify that the new branch is pushed properly in the remote server. The following image shows that the new branch, secondary, is pushed properly.

B. Upstream Branch Using -u Option

Run the following commands to create a new branch named testing using the -b option and push the new branch to the remote repository by using the -u option. Like the previous command, you have to authenticate the GitHub user account to push the newly created branch into the remote server.

$ git checkout -b testing
$ git push -u origin testing

The following output will appear if the GitHub account is authenticated properly.

You can check the remote repository from github.com to verify that the new branch is pushed properly in the remote server. The following image shows that the new branch, testing, is pushed properly.

Set Upstream Branch Using Alias:

The upstream branch task can be done easily by using the alias command. Git alias and Bash alias command can be used to push the newly created branch to the remote repository. The uses of these commands have shown in this part of this tutorial.

A. Upstream Branch Using Git Alias:

Run the first command to create the git alias command named pushd for pushing the newly created branch into the remote server. Here, pushing to HEAD indicates that the remote branch name and the local branch name will be the same. Run the second command to create a new branch named newBranch. Run the third command to push the newly created branch into the remote server by using the git alias command. Like the previous command, you have to authenticate the GitHub user account to push the newly created branch into the remote server.

$ git config --global alias.pushd "push -u origin HEAD"
$ git checkout -b newBranch
$ git pushd

The following output will appear if the GitHub account is authenticated properly.

B. Upstream Branch Using Bash Alias:

Run the first command to create the bash alias command named gp for pushing the newly created branch into the remote server. Here, HEAD indicates the same meaning of the git alias command. Run the second command to create a new branch named newBranch2. Run the third command to push the newly created branch into the remote server by using the bash alias command. Like the previous command, you have to authenticate the GitHub user account to push the newly created branch into the remote server.

$ alias gp='git push -u origin HEAD'
$ git checkout -b newBranch2
$ gp

The following output will appear if the GitHub account is authenticated properly.

You can check the remote repository from github.com to verify if the new branch is pushed properly in the remote server.

The following image shows that two new branches have been pushed in the remote repository. These are newBranch and newBranch2.

Conclusion:

Different ways to upstream the git branch from the local repository to the remote repository have been described in this tutorial by using a demo git repository. The newly created branches are pushed into the remote repository mainly by using the push command. This command is used in multiple ways in this tutorial to upstream the newly created git branch to the remote server for helping the readers to understand the way to set the upstream branch in the git.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.