How to Set Upstream in Git
In this article, we will learn how to use upstream
in Git. While working with Git by cloning and creating a new repository in a branch, we have to set upstream
branches for the future commits and fetch. But first, we should understand that what are upstream
branches and how it works. Upstream branches are linked strongly with remote branches.
upstream
branches can be defined as the branch followed on the remote repository by our local branch (also called the remote-tracking branch). When we need to set up the default remote branch as our current local branch, we have to run the git upstream
command.
Locally this command is used for set-upstream:
git branch --set-upstream <remote-branch>
Furthermore, options are also available that are mentioned below:
Set Upstream Branch Using Git Push
The simplest way to set the upstream branch is to use the git push
command with the -u
option used for the upstream branch. The following command will take the branch name to set the local branch as the remote branch.
$ git push -u <remote> <branch>
Rather than that, you can use the --set-upstream
option similar to the -u
option, which will set the local branch as the remote branch.
$ git push --set-upstream <remote> <branch>
Let’s take an example if we created a branch named branch
using the checkout command.
$ git checkout -b branch
It switched to a new branch branch
.
One way to avoid having to precise type --set-upstream
is to use its brief version -u
as follows:
git push -u origin local-branch
This will set the upstream association for any future push or pull commands easily. When we need to push to a remote repository and use the --set-upstream
explicitly in the command, it will set the branch in the local environment that we are pushing to as the remote-tracking branch.
If we add a remote branch and use the--set-upstream
in the command, it means that Git knows what we want to do when we Git fetch, Git pull, or Git push in the future. It keeps the local and remote branches up to date with tracking and maintaining the pull and push commands.
Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.
LinkedIn