How to Set Upstream Branch on Git?
Last Updated :
20 May, 2024
It is important to Setup an Upstream Branch in Git to make the workflow smooth and manage branches efficiently. When you want to clone a new repository or work with various feature branches, you need to know how to work with upstream branches and how you can set them up.
In this article, we will explore how to set up upstream branches and also see which git local branch is tracking which upstream remote branch.
Prerequisites
What is an Upstream Branch?
An upstream branch in Git refers to a branch that serves as a reference point for another branch. Typically, it’s used to track the remote branch, allowing developers to fetch updates, compare changes, and push their commits easily. Setting an upstream branch simplifies the process of keeping local and remote repositories in sync.
How to Set Upstream Branches in Git?
Using the Git Push command with the “-u” option for the upstream branch.
Set Upstream Branch using Git Push command
To set Upstream Branch using Git Push, you first have to Create a new branch with the name ” and switch to the current branch using the -b option
git checkout -b <branch name>
Switching the branch confirmation appears below:

Switching the branch confirmationÂ
When the current branch i.e (‘new_branch’) has no Upstream branch set and we try to run the command “Git push”. After running the below command in cmd:

Now, you need to set the upstream branch using the Git push command with the -u option. Â Replace <branch name> with your branch name.
git push -u origin <branch name>
 Alternatively, you can use the ‘–set-upstream’ command as well to set the Upstream branch
git push --set-upstream origin <branch name>

How to Change Upstream Branches in Git
Now, you need to track a new upstream branch than the one you just setup running:
git branch -u <remote/branch name>
For example:
git branch main -u <origin/new_branch>
git branch main -u <origin/main>
The terminal prints out the confirmation message:


How to check which Git Branches are tracking which Upstream Branches
Now To check which Git Branches are tracking which Upstream Branches,you can list all your branches that are tracking upstream branches using “Git branch” with the -vv option:
git branch -vv

The main branch has a tracking branch of [origin/main]. The test branch has a tracking branch of [origin/test]. The new_branch branch has a tracking branch of [origin/new_branch].
Conclusion
To set up and change upstream branches in Git is crucial for efficient collaboration and version control. This process ensures that your local branches are synchronized with their corresponding remote branches on platforms like GitHub or Bitbucket.
Similar Reads
How to Add Upstream in Git?
When you fork a repository, you create a copy of it under your own account. However, as the original repository updates with new features, bug fixes, and improvements, you'll want to keep your forked repository up-to-date with these changes. This is where adding an upstream remote in Git comes into
3 min read
How to Merge Two Branches in Git?
Version control systems like Git provide powerful tools for managing code changes and collaboration among developers. One common task in Git is merging branches, which allows you to combine the changes made in one branch into another. In this article, we will explore the process of merging branches
4 min read
How to Push Git Branch to Remote?
Git is the most popular version control system which records the changes made to our project over time in a special database called a repository. We can look at our project and see who has made what changes when and why and if we screw something up we can easily revert our project back to an earlier
6 min read
How to Rename Branch in Git?
Renaming branches in Git is important for keeping your repository organized and understandable, especially when collaborating with others. Clear and descriptive branch names improve project management and help team members identify the purpose of each branch. Renaming branches also ensures consisten
1 min read
How to List Remote Branches in Git?
Git is a powerful version control system that allows developers to collaborate on projects, maintain code history, and manage multiple lines of development through branching. One common task when working with Git is to list all branches in a remote repository. This article will guide you through the
3 min read
How to Remove a Remote Branch in Git?
Git is an essential tool for version control in modern software development. It allows multiple developers to collaborate on a project efficiently by managing changes to the source code. One common task when managing a Git repository is removing remote branches. This might be necessary when a featur
3 min read
What is Git-Ignore and How to Use it?
There are various types of files we might want the git to ignore before committing, for example, the files that are to do with our user settings or any utility setting, private files like passwords and API keys. These files are not of any use to anyone else and we do not want to clutter our git. We
5 min read
How To Upload a Project On GitHub?
Uploading your project to GitHub allows you to share your work with others, collaborate with team members, and keep your code safe and accessible. This article will walk you through the process of uploading a project to GitHub, ensuring that you can efficiently manage your code and contributions. Pr
4 min read
How To Delete Remote Branch in Git?
Git is an important tool in the process of application development and is used widely in the software industry by developers to maintain the codebase. Using this developers are able to organize their codebase and manage the version history of their project. Now, as a developer, you need to know how
1 min read
How to Merge a Git Branch into Master?
If you're working with Git, merging branches is a fundamental task. Whether you're adding new features, fixing bugs, or improving documentation, merging ensures that your changes are integrated into the main codebase. In this guide, we'll walk you through the process of merging a Git branch into the
3 min read