Make Selenium Easy

Git Tutorial 16 – What Is Upstream Branch In Git and How To Set it?

Introduction

As a part of GIT Tutorials End To End, we will learn about the tracking and upstream branch in Git in this post.

Prerequisite posts

I will expect that you are aware of the basic concepts and commands of GIT. But if you are a beginner in GIT then I strongly recommend you to refer GIT Basic Commands and Concepts section on my blog first.

Did you know that I have started a YouTube channel as well and I need your support to make it successful. Please do watch content then comment, like, share, and obviously subscribe.

What is an Upstream branch in Git?

I see this term confuses many beginners in Git. Let’s understand the scenario.

Let’s clone one of my public repo to practice and learn easily. The same repo I have used in my previous posts. In this repo, I have only the default branch “main” and two commits.

Let’s create a new branch.

Now switch to the new branch and try to execute “git pull” and “git push” commands.

In previous posts, we have executed the above git commands and we didn’t face the above issues but for a new branch, we are getting it. Let’s understand the error message as that explains the problem and solution both.

When you apply for a new job then generally an HR person will contact you to share details and take inputs from you. You both will be communicated with each other for any help. A similar scenario in the above branch. We have created a new branch locally and it is not linked with any branch in the remote repository. That is the reason when we give pull commands, my new branch is not aware of where to pull details from and for push command, it is not aware of where to push. In short, it is complaining of a missing upstream branch in the remote repo. Without mapping a local branch to a remote branch we can not perform push and pull operations.

Execute “git branch -vv” command and check the output. The first line in output for the branch “main” shows “[origin/main]” but there is no such information is shown for the new branch “myNewBranch”. This command shows the information on upstream branches for each local branch.

When you clone a remote repository it automatically creates a local branch whose name is the same as the remote repo i.e. “main” that tracks “origin/main” in the remote repo. In other words, we have a local branch called “main” whose upstream/tracking branch is “origin/main” after cloning. To verify you can check out to “main” branch and execute the git pull and push command. You will not see any upstream branch errors like the new branch.

So now we are clear that our new branch has no upstream branch and pull and push can not be performed. We need to set it explicitly. We can see suggested commands in error but ignore them as of now. You can set any remote branch as a tracking/upstream branch. Now you may think that we have only one remote branch i.e. “main” so can more than one local branches have the same remote branch? Yes, it is possible but that will not make any sense here. When we create a new branch we should push that branch to remote which will create a new remote branch and set this new remote branch as a tracking/upstream branch. Actually same we have done in the previous post.

git push -u origin <newBranchName>

“-u” is shorthand of “–set-upstream”.

You can see that now the new branch has a remote-tracking branch and you can do pull and push operations.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe to my YouTube channel.
#ThanksForReading
#HappyLearning

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

1 thought on “Git Tutorial 16 – What Is Upstream Branch In Git and How To Set it?

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

Git Tutorial 16 – What Is Upstream Branch In Git and How To Set it?

by Amod Mahajan time to read: 3 min
1
%d bloggers like this: