Class 23 of 50 • Remote Collaboration

Git Upstream & Tracking Branches: Remote Linkage and Configuration

Have you ever wondered why running git status can tell you that "Your branch is ahead of 'origin/main' by 2 commits"? This magic is powered by Remote-Tracking Branches and Upstream Linkage.

What is a Remote-Tracking Branch?

Git maintains three kinds of branches:

  1. Local Branches: Pointers you can checkout, edit, and commit to (e.g., main, feature/auth).
  2. Remote Branches: Branches physically existing on the remote GitHub server.
  3. Remote-Tracking Branches: Local, read-only bookmarks representing the state of remote branches the last time you communicated with the remote (e.g., origin/main). You cannot switch to or commit to them directly.

Inspecting Branch Tracking Linkage (git branch -vv)

To inspect which local branches are tracking which remote branches, run:

$ git branch -vv
* main         8a2f1b4 [origin/main: ahead 2, behind 1] feat: add auth
  feature/cart 4b9e2c1 [origin/feature/cart] feat: add checkout
  hotfix-patch 19d8a3f fix: repair broken link

Notice the detailed status: [origin/main: ahead 2, behind 1] means you have 2 local commits not yet pushed, and the remote server has 1 new commit you haven't fetched!

Setting or Changing the Upstream Branch

If you created a local branch and forgot to pass -u during your first push, configure it explicitly:

# Explicitly set upstream tracking:
$ git branch --set-upstream-to=origin/main main

# Or pass -u during your next push:
$ git push -u origin main

Checking Out Remote Branches Directly

If a colleague pushed a new branch named feature/dashboard to GitHub, how do you work on it locally?

# Step 1: Fetch remote branch metadata
$ git fetch origin

# Step 2: Switch to the branch name
$ git switch feature/dashboard
Branch 'feature/dashboard' set up to track remote branch 'feature/dashboard' from 'origin'.
Switched to a new branch 'feature/dashboard'

Modern Git automatically creates a local branch matching the remote name and links it as a tracking branch in one command!

Explore Related Git & GitHub Tutorials & Next Steps

Strengthen your version control mastery by studying these connected classes:

Push, Pull & Fetch Operations

Run shorthand git push and git pull commands backed by upstream tracking.

Managing Remote Origins

Inspect configured remote fetch and push specifications in .git/config.

Branching Fundamentals

Inspect remote branches with git branch -r and all branches with git branch -a.

Forking & Syncing Upstream

Track both personal origin fork and upstream open-source repositories.

Frequently Asked Questions (FAQ) - Student & Interview Review

It indicates divergent branches. You have created 1 local commit that does not exist on GitHub, and your teammates have pushed 2 commits to GitHub that you haven't merged into your local branch yet.
Run `git branch --unset-upstream [branch-name]`. This breaks the linkage, making the branch an independent local-only branch.
Yes. For example: `git branch --set-upstream-to=origin/develop my-local-feature`. However, keeping local and remote branch names identical avoids confusion.

Next Step in Your Git & GitHub Learning Path:

Advance your software engineering skills with the next topic in our synchronized curriculum.

Continue to Tagging & Releases → Previous: ← push, fetch & pull | Full Syllabus

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!