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.
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 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.
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 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 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.
$ 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.