Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • Git Tutorial
  • Git Exercises
  • Git Basic Commands
  • Git Cheat Sheet
  • Git Interview Questions
  • Git Bash
  • GitHub
  • Git Branch
  • Git Merge
  • Git WorkFlow
  • Git Hooks
  • Git LFS
  • Git Rebase
  • Git Cherry Pick
Open In App
Next Article:
How to Make an Existing Git Branch Track a Remote Branch?
Next article icon

How to Make an Existing Git Branch Track a Remote Branch?

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

Managing branches in Git can sometimes be confusing, especially when you need to make an existing local branch track a remote branch. This process ensures that your local branch can easily fetch updates from and push changes to the corresponding remote branch. In this article, we will walk through the steps to set up tracking for an existing Git branch.

Understanding Tracking Branches

A tracking branch in Git is a local branch that has a direct relationship with a remote branch. When a local branch is set to track a remote branch, it becomes easier to synchronize changes between your local repository and the remote repository. This allows you to use commands like git pull and git push without specifying the remote branch explicitly.

Steps to Make an Existing Git Branch Track a Remote Branch

Here’s a step-by-step guide to make an existing local Git branch track a remote branch:

Step 1: Verify Your Current Branch

Start by checking the branch you are currently on. You can do this with:

git branch

The currently active branch will be highlighted with an asterisk (*).

Step 2: Switch to the Branch You Want to Track

If you are not already on the branch you want to set up, switch to it using:

git checkout <your-branch>

Replace <your-branch> with the name of your local branch.

Step 3: Set Up Tracking with --set-upstream-to

Use the git branch command with the --set-upstream-to option to configure the local branch to track the remote branch.

git branch --set-upstream-to=<remote>/<remote-branch>

Replace <remote> with the name of your remote repository (commonly origin), and <remote-branch> with the name of the remote branch you want to track.

For example, if you want to set your local branch feature to track the remote branch feature on the origin remote, you would use:

git branch --set-upstream-to=origin/feature

Step 4: Verify Tracking

After setting up the tracking, you can verify that your local branch is now tracking the remote branch:

git branch -vv

This command shows detailed information about all your branches, including the tracking information. You should see something like this:

* feature   a1b2c3d [origin/feature] Commit message here

The [origin/feature] part indicates that the local feature branch is tracking the feature branch on the origin remote.

Example:

Update local metadata:

Fetch the latest changes from all remote branches.

git fetch --all

View remote and local branches:

List all local and remote branches.

git branch -a

Switch to the target branch:

Change to the branch you want to link with the remote branch.

git checkout <branch-name>

Link the local branch to a remote branch:

Set the upstream branch for the local branch to track.

git branch --set-upstream-to=origin/<remote-branch>

Verify tracking:

Check the tracking status of your branches.

git branch -vv

Conclusion

Setting an existing Git branch to track a remote branch can simplify your workflow by allowing you to pull and push changes without repeatedly specifying the remote and branch names. By following the steps outlined above, you can easily configure your local branches to track the corresponding remote branches, enhancing your productivity and ensuring smoother synchronization with your remote repository.


Next Article
How to Make an Existing Git Branch Track a Remote Branch?

S

swati4934
Improve
Article Tags :
  • Git

Similar Reads

    How to Make the Current Git Branch a Master Branch?
    Managing branches effectively is a key part of using Git for version control. Occasionally, you might find yourself in a situation where the current branch you're working on should be the new master branch. This could be due to the current branch evolving to become the main line of development, or s
    3 min read
    How To Change the Remote a Branch is Tracking in Git?
    In Git, branches allow multiple developers to work on different features or fixes simultaneously. Often, a branch is set to track a remote branch, which means that Git can keep track of which commits have been pushed to or pulled from that remote branch. There might be situations where you need to c
    3 min read
    How to Compare a Local Git Branch with its Remote Branch ?
    When working with Git, it's often necessary to compare your local branch with its remote counterpart to understand the differences in terms of commits and changes. This helps in keeping your local work in sync with the remote repository and managing potential conflicts. This article will guide you t
    3 min read
    How To Find Out Which Remote Branch A Local Branch Is Tracking?
    Git is a powerful version control system that allows developers to manage and track changes in their code repositories. One of the key features of Git is its ability to track relationships between local and remote branches. Knowing which remote branch a local branch is tracking can help you manage y
    3 min read
    How to Create a Remote Git Branch?
    Creating a remote Git branch allows multiple developers to work on different features or fixes simultaneously. It ensures a clean and organized workflow, enabling collaboration without affecting the main codebase. This article covers the steps to create a remote Git branch and push it to a remote re
    2 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