Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • jQuery
  • AngularJS
  • ReactJS
  • Next.js
  • React Native
  • NodeJS
  • Express.js
  • MongoDB
  • MERN Stack
  • PHP
  • WordPress
  • Bootstrap
  • Tailwind
  • CSS Frameworks
  • JS Frameworks
  • Web Development
Open In App
Next Article:
How to Add Upstream in Git?
Next article icon

How to Set Upstream Branch on Git?

Last Updated : 20 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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

  • Git is installed and configured on your local machine.
  • A cloned Git repository or your own Git project exists locally

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-branch

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:

switching-branch

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>

use-set-upstream-command

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:

confirmation-messageconfirmation-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

git-branching

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.



Next Article
How to Add Upstream in Git?

D

deepeshnagpal
Improve
Article Tags :
  • Geeks Premier League
  • Git
  • How To
  • Web Technologies
  • Geeks-Premier-League-2022

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 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 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 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
  • How To Publish A New Branch In Git?
    Git is a distributed version control system, offering powerful tools for collaboration, version tracking, and branching. One of its key features is branching, which allows developers to work on different features or fixes simultaneously without interfering with the main codebase. This article will g
    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
  • How To Fetch Remote Branches in Git ?
    Git provides a powerful version control system that allows developers to collaborate efficiently on projects. Over time, however, a repository can have local and remote branches. Local branches are created locally and only exist on local machines and the remote branches exist on the remote repositor
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences