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.
Git maintains three kinds of branches:
main, feature/auth).origin/main). You cannot switch to or commit to them directly.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!
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
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!
Strengthen your version control mastery by studying these connected classes:
Run shorthand git push and git pull commands backed by upstream tracking.
Inspect configured remote fetch and push specifications in .git/config.
Inspect remote branches with git branch -r and all branches with git branch -a.
Track both personal origin fork and upstream open-source repositories.
Advance your software engineering skills with the next topic in our synchronized curriculum.
Continue to Tagging & Releases → Previous: ← push, fetch & pull | Full Syllabus