CurriculumRemote RepositoriesTracking Upstream Branches
Lesson 6 of 786%
Intermediate6 min+60 XP

Tracking Upstream Branches

Upstream tracking is a relationship between a local branch and a remote branch. Once set, git push, git pull, and git status all automatically know which remote branch to use — no need to type "origin main" every time.

01.What Tracking Does For You

When a local branch tracks a remote branch, git status shows "Your branch is 2 commits ahead of origin/main". git push knows where to send your commits. git pull knows where to pull from. No more typing "origin main" every single time. This tracking relationship is stored in .git/config per branch.

Branch Visualization
featuremainHEAD
main
feature
HEAD

02.Viewing and Setting Tracking

git branch -vv shows all local branches with their tracking relationship and ahead/behind counts. If a branch has no tracking, set it with git branch --set-upstream-to or by using the -u flag on your next push. Cloning sets this up automatically for the default branch.

Distributed Architecture
💻
Your Local
push / pull
☁️
GitHub (Remote)
push / pull
💻
Teammate's Local

Commands

commands — click to reveal output
$
git branch -vv
Show all branches with upstream tracking info
▼ show output
$
git push -u origin feature/login
Push AND set upstream in one command
$
git branch --set-upstream-to=origin/main main
Set upstream for an existing branch
$
git branch --unset-upstream
Remove upstream tracking from current branch
$
git status
After tracking is set, shows ahead/behind info
▼ show output
KNOWLEDGE CHECK

What is the main benefit of setting an upstream tracking branch?

Rate this lesson:
PreviousNext Lesson