Upstream and Remote-Tracking Branches
A weather app shows "Last updated 10 minutes ago" next to the forecast. The numbers on your screen are a copy of the weather service's data, as it was the last time the app checked. To see the newest forecast, you pull to refresh.
Remote-tracking branches like origin/main are that copy. They record where the server's branches were the last time you fetched. And each local branch can have an upstream: the remote-tracking branch it is paired with, which git pull, git push and git status use by default. Once you understand these two ideas, messages like "ahead of 'origin/main' by 1 commit" and "have diverged" become simple to read.
In this lesson, you will see both kinds of branch, read ahead and behind counts, and set up or change the pairing between them.
The Starting Point
The server fee-tracker.git and clones for Navika and Advitha. Navika commits locally without pushing, while Advitha pushes two commits:
git init --bare --quiet fee-tracker.git
mkdir navika
cd navika
git init --quiet fee-tracker
cd fee-tracker
echo "Fee Tracker" > README.md
echo 'Term 1 fee: $12,000' > fees.txt
git add .
git commit --quiet -m "Add README and the term fee"
git remote add origin ../../fee-tracker.git
git push --quiet -u origin main
cd ../..
git clone --quiet fee-tracker.git advitha/fee-tracker
cd advitha/fee-tracker
git config user.name "Advitha Anamaneni"
git config user.email "advitha@example.com"
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "Add the late fee"
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add the hostel fee"
git push --quiet
cd ../../navika/fee-tracker
echo "Run the app to see the fees." >> README.md
git commit --quiet -am "Explain how to run the app"
git fetch --quiet
git log --oneline --graph --all* 49ad108 (HEAD -> main) Explain how to run the app
| * da47095 (origin/main, origin/HEAD) Add the hostel fee
| * badb379 Add the late fee
|/
* c460dae Add README and the term feeTwo Kinds of Branches
Navika's repository now has two branches that sound alike:
| Branch | What it is | Who moves it |
|---|---|---|
main | Her local branch, where she commits | Her commits, merges and resets |
origin/main | A remote-tracking branch: her copy of the server's main as of the last fetch | Only git fetch, git pull and git push |
You can read, compare, log and merge origin/main, but you never commit on it. git switch origin/main would put you in detached HEAD.
The Upstream
Each local branch can be paired with one remote-tracking branch, its upstream (also called its tracking branch). Navika's main has origin/main as its upstream, set when she pushed with -u. git branch -vv (two vs) shows each branch's upstream and how they compare:
git branch -vv* main 49ad108 [origin/main: ahead 1, behind 2] Explain how to run the app[origin/main: ahead 1, behind 2] says: main has 1 commit that origin/main doesn't have (Navika's README commit), and origin/main has 2 commits that main doesn't have (Advitha's fees). git status says the same in words:
git status | head -4On branch main
Your branch and 'origin/main' have diverged,
and have 1 and 2 different commits each, respectively.
(use "git pull" if you want to integrate the remote branch with yours)| Status | Meaning | Usual next step |
|---|---|---|
up to date | Same commit | Nothing to do |
ahead ... by N | You have N commits to push | git push |
behind ... by N | The server has N commits you don't | git pull |
have diverged | Both have new commits | git pull (merge or rebase), then git push |
Use the Upstream by Name: @{u}
@{u} (short for @{upstream}) means "the upstream of the current branch". It saves typing origin/main, and works on any branch:
git log --oneline @{u}..
git log --oneline ..@{u}49ad108 (HEAD -> main) Explain how to run the app
da47095 (origin/main, origin/HEAD) Add the hostel fee
badb379 Add the late fee@{u}..means "in my branch but not in the upstream": the commits waiting to be pushed...@{u}means "in the upstream but not in my branch": the commits waiting to be pulled.
Navika pulls and pushes to get back in step:
git pull --quiet --rebase
git push --quiet
git branch -vv* main 2d78edc [origin/main] Explain how to run the appNo count in the brackets means up to date.
Set, Change or Remove an Upstream
A branch created locally has no upstream until you give it one:
git switch --quiet -c feature/receipts
git pullThere is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> feature/receiptsThere is no tracking information for the current branch. Git doesn't know where to pull from. There are three ways to set an upstream:
| Command | When |
|---|---|
git push -u origin <branch> | The branch isn't on the server yet: push it and set the upstream together |
git branch -u origin/<branch> | The branch already exists on the server: just pair them |
git switch <branch> (with only origin/<branch> existing) | Creating the local branch from a server branch sets it automatically |
Here, the branch isn't on the server, so Navika pushes with -u:
echo "Receipt No. 1" > receipts.txt
git add receipts.txt
git commit --quiet -m "Add receipt numbers"
git push --quiet -u origin feature/receipts
git branch -vv* feature/receipts 0ffd4f5 [origin/feature/receipts] Add receipt numbers
main 2d78edc [origin/main] Explain how to run the appTo pair a branch with a different upstream, run git branch -u <remote>/<branch> again. To remove the pairing, git branch --unset-upstream:
git branch --unset-upstream
git branch -vv* feature/receipts 0ffd4f5 Add receipt numbers
main 2d78edc [origin/main] Explain how to run the appThe brackets are gone from feature/receipts. It is still on the server, but git pull and git push no longer know where to go for it. git branch -u origin/feature/receipts would pair them again.
Syntax
| Command | It is used to |
|---|---|
git branch -vv | Show each branch's upstream with ahead/behind counts |
git push -u origin <branch> | Push and set the upstream |
git branch -u origin/<branch> | Set the current branch's upstream (long form --set-upstream-to) |
git branch --unset-upstream | Remove the current branch's upstream |
git log @{u}.. | List commits not yet pushed |
git log ..@{u} | List commits not yet pulled |
git rev-parse --abbrev-ref @{u} | Print the name of the upstream |
Common Mistakes
Trusting ahead/behind without fetching. The numbers compare with origin/main as last fetched. Fetch first.
Treating origin/main as a branch to work on. It moves only on fetch, pull and push. Work on main.
Using git branch -u for a branch that isn't on the server. Git refuses, because origin/<branch> doesn't exist yet. Push with -u instead.
Confusing the old option --set-upstream with --set-upstream-to. git branch --set-upstream was removed long ago. Use -u or --set-upstream-to.
Expecting a new local branch to pull from main's upstream. Each branch has its own upstream, or none.
Under the Hood
Remote-tracking branches are stored next to local ones, under refs/remotes/<remote>/. The upstream is two lines in .git/config for each branch:
git switch --quiet main
git for-each-ref --format="%(refname)" refs/heads refs/remotes
git config --get-regexp "^branch\.main\."refs/heads/feature/receipts
refs/heads/main
refs/remotes/origin/HEAD
refs/remotes/origin/feature/receipts
refs/remotes/origin/main
branch.main.remote origin
branch.main.merge refs/heads/mainbranch.main.remote origin says which remote, and branch.main.merge refs/heads/main says which branch on that remote. Git combines them with the remote's refspec to find refs/remotes/origin/main, which is what @{u} resolves to. Ahead and behind are counted by walking both histories back to their merge base, exactly as git merge-base would.
Interview Corner
What is a remote-tracking branch?
A local, read-only record of where a branch on a remote was at the last fetch, such as origin/main, stored under refs/remotes/. It is updated only by git fetch, git pull and git push.
What is an upstream (tracking) branch?
The remote-tracking branch that a local branch is paired with. It is the default for git pull and git push, and what git status and git branch -vv compare with to show ahead/behind counts.
What does "Your branch and 'origin/main' have diverged" mean?
Your local branch and its upstream each have commits the other doesn't. You need to integrate with git pull (merge or rebase) before you can push.
How do you set the upstream of an existing branch?
git branch -u origin/<branch> (or --set-upstream-to) if the branch exists on the remote. If not, git push -u origin <branch> creates it and sets the upstream.
How do you see commits that haven't been pushed yet?
git log @{u}.. (or git log origin/main..main). git status and git branch -vv show the count.
Practice
Challenge: read the counts. In Navika's clone on main, make two local commits without pushing (add Exam fee: $800 and then Sports fee: $700 to fees.txt). Print git branch -vv and the commits waiting to be pushed.
What does Git print? Before you open the solution, decide what the brackets after main say, and how many commits git log @{u}.. lists.
Show solution
echo 'Exam fee: $800' >> fees.txt
git commit --quiet -am "Add the exam fee"
echo 'Sports fee: $700' >> fees.txt
git commit --quiet -am "Add the sports fee"
git branch -vv
git log --oneline @{u}.. feature/receipts 0ffd4f5 Add receipt numbers
* main dfdd4fd [origin/main: ahead 2] Add the sports fee
dfdd4fd (HEAD -> main) Add the sports fee
c6fd59b Add the exam feemain is [origin/main: ahead 2]: two local commits, nothing new from the server as far as the last fetch knows. git log @{u}.. lists exactly those two. feature/receipts still has no upstream, from the previous step.
Checkpoint. In a cloned repository, create a branch, push it with -u, and check git branch -vv. Unset the upstream, see git pull refuse, then set it again with git branch -u.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git branch -vv | Show upstreams with ahead/behind counts |
git branch -r | List remote-tracking branches |
git push -u origin <branch> | Push and set the upstream |
git branch -u origin/<branch> | Set the upstream of the current branch |
git branch --unset-upstream | Remove the upstream |
git log @{u}.. | Commits waiting to be pushed |
git log ..@{u} | Commits waiting to be pulled |
Was this tutorial helpful?