Git

Why Do I Need to Do “–set-upstream” All the Time?

While working on Git, when developers want to clone a remote repository or work with multiple feature branches, they need to set tracking connections between local and remote branches to set them as upstream branches. It can be established while pushing the particular local branch to GitHub hosting service. For this purpose, the “–set-upstream” or “-u” options can be utilized with the “$ git push” command.

This guide will discuss:

So, let’s start!

Why Do I Require to Do “–set-upstream” All the Time?

To build a tracking connection between the remote and local repositories, you need to “–set-upstream” for the first time while pushing the local branch to the Git remote branch.

How to Build Tracking Connection Using the “–set-upstream” Option?

To set a tracking connection, firstly, navigate to the Git directory where the desired repository is located. Then, create and switch to the branch immediately. Clone Git remote repository and push the current branch to it. Lastly, run the “$ git push –set-upstream origin <local-branch>” command to set a tracking connection between local and remote branches.

Let’s implement the above-provided procedure for better understanding!

Step 1: Move to Directory
First, navigate to the Git local repository using the following command:

$ cd "C:\Users\nazma\Git\Test_1"

Step 2: Create and Switch Branch
Run the “git checkout” command with the “-b” option to create and switch Git local branch simultaneously:

$ git checkout -b alpha1

Step 3: Clone Remote Repository
To clone Git remote repository, run the “git clone” command along with the remote URL:

$ git clone https://github.com/GitUser0422/Test_repo.git



Step 4: Git Push

Push the current Git local branch by utilizing the “git push” command:

$ git push

Without setting the upstream, you will encounter the following error:

Step 5: Set Upstream Branch
To set an upstream local branch while performing a push operation from the local to the remote repository for the first time, run the “git push” command with the “–set-upstream” option:

$ git push --set-upstream origin alpha1

The below-given output indicates that the local branch is set as an upstream branch:

We have offered the method to set the local branch as an upstream branch while pushing into the remote repository.

Conclusion

To set a tracking connection between local and remote repositories, firstly, move to the Git local repository. Then, create and switch to the branch immediately. Clone Git remote repository and push the current branch to it. Lastly, run the “$ git push –set-upstream origin <local-branch>” command on the bash terminal. This guide explained why we must “–set-upstream” all the time.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.