Git

Why do I have to “git push –set-upstream origin “?

While working on Git, developers need multiple branches, which is a beneficial feature of the decentralized system. It allows developers to test in the Git remote developer’s environment that no one else can display unless the branch is explicitly shared with others. For this purpose, it is required to push the local branch to the remote repository to set the remote as the upstream directory.

This blog discusses the procedure to set the remote as the upstream directory.

Why do I have to “git push –set-upstream origin <branch>”?

To set upstream the local branch, firstly, move to the local directory and clone the remote repository to the Git local repository. Execute the “$ git switch -c <branch-name>” command to create a new branch and switch to it simultaneously. Lastly, push the local branch to the remote repository to set the remote as the upstream directory by executing the “$ git push –set-upstream origin <branch>” command.

Now, practically perform the above-given scenario!

Step 1: Navigate to Git Repository

First, navigate to the Git directory where the local repository is located by executing the “cd” command:

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

Step 2: Clone Remote Repository

Then, execute the “git clone” command by specifying the remote repository URL to clone the Git remote repository to Git local repository:

$ git clone https://github.com/GitUser0422/Linux-repo.git

Step 3: git switch

Create a new local branch and switch to it by executing the “git switch” command:

$ git switch -c new_branch

Here, the “-c” option is used for creating a new branch and switch immediately:

Step 4: git push

Now, run the “git push” command to push the local branch to the remote hosting server GitHub:

$ git push

As you can see, the local branch is not pushed to the remote repository because it has not set to upstream:

Step 5: Branch Set Upstream

Run the “git push” command along with the “–set-upstream” option:

$ git push --set-upstream origin new_branch

Here, the “–set-upstream” option is utilized to set the remote as the upstream directory and fix the above-encountered error:

We have demonstrated why do I have to “git push –set-upstream origin <branch>”.

Conclusion

To set upstream the local branch, firstly, move to the Git local repository and clone the remote repository. After that, execute the “$ git switch -c <branch-name>” command to create and switch to it simultaneously. Push the local branch to the remote repository to set the remote as the upstream directory by executing the “$ git push –set-upstream origin <branch>” command. This blog illustrated the method to set the remote as the upstream directory.

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.