DEV Community

Cover image for Understanding the Git Upstream Tracking
Prashant Dwivedi
Prashant Dwivedi

Posted on

Understanding the Git Upstream Tracking

When using the git for version control and GitHub as the remote repo then we often get an error when we run the following command;

  • git pull
  • git push Image description

Now the question is what is this upstream?

What is Upstream Tracking?

To understand this let us say you have one remote repository and one local repository. The local repository has the remote connection with remote repository on GitHub. Now, lets say we made a local branch named branch_1 and we want to push this new branch to remote repository for this we simply pushed it to remote repo using the command

  • git push origin branch_1

Now our remote repository has the branch named branch_1 Till this moment of time nothing feels bad, unless we again make some changes in our local branch_1 and push it to remote by simply writing the following command

  • git push

This is the moment we get the above error saying that "there is no tracking information for current branch".

This simply means that there is no "Upstream Tracking Branch" for your current local branch.

What is an Upstream Branch?

Upstream branches are the branch that used to track the remote branches by your local branch. To make it simple, if you current local branch named branch_1 had the upstream tracking then we will have one branch named remotes/origin/branch_1 or simply origin/branch_1. Whereas, if the tracking is not set then we don't get any such branch.

Note: Don't confuse the remote/branch_1 as the remote repository branch, this is not the remote repo branch but just a latest copy of that remote branch.

These upstream branches are used to keep up with latest changes.

Why we got those errors?

Now that we have some understanding of upstream tracking, we can understand the cause of those errors.

  • git push: When we simply do git push, then since our current branch is not tracking any remote branch, it does not know to which branch to push these changes in remote and thus we get that error.
  • git pull: When we do git pull, then since our current branch is not tracking any remote branch, it does not know form which branch to pull the latest changes.

Once we have our upstream tracking set, we can run simple git pull and git push commands without any error because now the current local branch knows which branch it is associated with in the remote repository.

How to check the upstream tracking status for any branch?

We can check the upstream tracking status for all the branches using the following command

  • git branch -vv Image description

Here we can see that

  • branch named "branch" has no upstream tracking, but
  • master branch is having the upstream tracking branch origin/master

How to set up the upstream tracking for any branch?

We can se the upstream tracking for any branch with following simple method

  • When we are pushing our branch to remote repository just use "-u"

       git push -u origin <branch_name>
    

Top comments (0)