
Module 5 Lesson 4: Tracking remote branches
Understand the link between local and remote. Learn how 'Upstream Tracking' simplifies your workflow and how to manage branches that live in both worlds.
Module 5 Lesson 4: Tracking remote branches
In preceding lessons, we used commands like git push origin main. But why do we have to type origin main every time? The answer lies in Tracking Branches.
In this lesson, we learn how to create a permanent link between your local branch and a remote branch.
1. What is a Tracking Branch?
A tracking branch is a local branch that has a direct relationship with a remote branch. When you are on a tracking branch and run git status, Git can tell you:
"Your branch is ahead of 'origin/main' by 2 commits" or "Your branch is behind..."
2. Setting Upstream Tracking
As we saw in Lesson 2, you set this up with the -u flag:
git push -u origin feature-auth
From now on, Git knows that feature-auth (local) is linked to feature-auth (on origin). You can now just type git push or git pull without any arguments.
Changing Tracking for an existing branch
If you already have a branch and want to link it to a remote:
git branch --set-upstream-to=origin/main main
3. Creating a Local Branch from a Remote
If a teammate creates a new branch called feature-ui and pushes it to the server, you won't have it locally yet. To get it:
git fetch: Get the info about the new branch.git switch feature-ui: Git will see that you don't have it locally, butorigin/feature-uiexists. It will automatically create a localfeature-uiand set up tracking for you!
4. Seeing Your Tracking Configuration
To see exactly which local branches are tracking which remote branches:
git branch -vv
Output Example:
main[origin/main] ...feature-x[origin/feature-x: ahead 2]hotfix...
graph LR
subgraph "Local Repo"
L_Main["main (Local Port)"]
end
subgraph "Remote Repo (origin)"
R_Main["main (Cloud Port)"]
end
L_Main -- "Tracks" --> R_Main
L_Main -. "git status reports diff" .-> R_Main
Lesson Exercise
Goal: Audit your tracking.
- Run
git branch -vvin your project. - Do you have any branches that aren't tracking a remote? (Usually these won't have the
[origin/...]bracket). - Create a dummy branch
test-tracking. - Try to run
git push. Git should give you an error: "The current branch test-tracking has no upstream branch." - Run the command Git suggests:
git push --set-upstream origin test-tracking. - Run
git branch -vvagain. Notice the new link is established.
Observation: You'll see that tracking is the "glue" that makes collaboration feel seamless. Without it, you'd have to tell Git where to go for every single transaction.
Summary
In this lesson, we established:
- Tracking branches create a link between local and remote.
git push -uis the primary way to establish this link.git statususes tracking information to tell you if you are "Ahead" or "Behind" the server.git branch -vvis the map of your repository's connections.
Next Lesson: When you're done with a feature, you delete it locally. But how do you Delete remote branches from the server?