Upstream and Remote-Tracking Branches

Updated · 10 min read
mediumGit 2.53gitupstreamremote-trackingahead-behindinterview

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:

Terminal
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
Output
* 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 fee

Two Kinds of Branches

Navika's repository now has two branches that sound alike:

BranchWhat it isWho moves it
mainHer local branch, where she commitsHer commits, merges and resets
origin/mainA remote-tracking branch: her copy of the server's main as of the last fetchOnly 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.

main, origin/main and the server's main

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:

Terminal
git branch -vv
Output
* 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:

Terminal
git status | head -4
Output
On 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)
StatusMeaningUsual next step
up to dateSame commitNothing to do
ahead ... by NYou have N commits to pushgit push
behind ... by NThe server has N commits you don'tgit pull
have divergedBoth have new commitsgit 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:

Terminal
git log --oneline @{u}..
git log --oneline ..@{u}
Output
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:

Terminal
git pull --quiet --rebase
git push --quiet
git branch -vv
Output
* main 2d78edc [origin/main] Explain how to run the app

No 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:

Terminal
git switch --quiet -c feature/receipts
git pull
Output
There 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/receipts

There is no tracking information for the current branch. Git doesn't know where to pull from. There are three ways to set an upstream:

CommandWhen
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:

Terminal
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
Output
* feature/receipts 0ffd4f5 [origin/feature/receipts] Add receipt numbers
  main             2d78edc [origin/main] Explain how to run the app

To pair a branch with a different upstream, run git branch -u <remote>/<branch> again. To remove the pairing, git branch --unset-upstream:

Terminal
git branch --unset-upstream
git branch -vv
Output
* feature/receipts 0ffd4f5 Add receipt numbers
  main             2d78edc [origin/main] Explain how to run the app

The 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

CommandIt is used to
git branch -vvShow 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-upstreamRemove 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:

Terminal
git switch --quiet main
git for-each-ref --format="%(refname)" refs/heads refs/remotes
git config --get-regexp "^branch\.main\."
Output
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/main

branch.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
Terminal
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}..
Output
  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 fee

main 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.

Quick check: Upstream branches

4 questions
1What is origin/main?
2git branch -vv shows [origin/main: ahead 1, behind 2]. What does it mean?
3Which command lists commits that haven't been pushed?
4A new branch exists only locally. How do you give it an upstream?

Key Takeaways

Quick reference

CommandIt is used to
git branch -vvShow upstreams with ahead/behind counts
git branch -rList 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-upstreamRemove the upstream
git log @{u}..Commits waiting to be pushed
git log ..@{u}Commits waiting to be pulled

Was this tutorial helpful?

Lesson 40 of 106