Sub topic 8
Upstream branches and push -u
This chapter assumes you can create and switch branches, make commits, and read git log. You should also know what a remote-tracking branch such as origin/main is and how git fetch updates it.
What an upstream branch is
An upstream branch is the remote-tracking branch that a local branch is paired with. Git stores the pairing in your repository config. Once a branch has an upstream, Git knows three things without being told:
- Which remote
git pushandgit pullshould talk to. - Which remote branch
git pullshould integrate. - How far your branch is ahead of or behind that remote branch.
Without an upstream, a bare git push or git pull has nothing to go on and fails or asks you to be explicit.
Setting the upstream with push -u
A new local branch has no counterpart on the remote. The first push creates one, and the -u flag (short for --set-upstream) records the pairing in the same step.
git switch -c feature/login
git commit -am "Add login form"
git push -u origin feature/loginThe last command does two things. It creates feature/login on origin, and it sets origin/feature/login as the upstream of your local feature/login. From now on, git push and git pull work with no arguments while you are on that branch.
Git writes the pairing into .git/config:
[branch "feature/login"]
remote = origin
merge = refs/heads/feature/loginremote names the remote. merge names the branch on that remote that git pull integrates.
Checking which upstream a branch has
git branch -vv lists each local branch with its upstream and its ahead/behind counts.
git branch -vvA line in the output looks like this:
* feature/login 3f9a1c2 [origin/feature/login: ahead 1] Add validation
main 8b2d7e4 [origin/main] Merge pull request #12ahead 1 means you have one commit that the remote lacks, so a push would send it. behind 2 would mean the remote has two commits you have not integrated. ahead 1, behind 2 means the histories have diverged. The counts compare against your last fetch, so run git fetch first if you want current numbers.
To see only the current branch's upstream, use:
git rev-parse --abbrev-ref --symbolic-full-name @{u}@{u} is shorthand for the upstream of the current branch. It also works in other commands, for example git log @{u}..HEAD lists the commits you have not pushed.
Changing or setting an upstream later
You can set the upstream on an existing branch without pushing:
git branch --set-upstream-to=origin/feature/login
git branch --unset-upstreamThe first command pairs the current branch with the named remote-tracking branch. The second removes the pairing.
When you check out a branch that exists only on the remote, Git sets the upstream for you:
git fetch origin
git switch feature/loginIf no local feature/login exists but exactly one remote has a branch with that name, git switch creates the local branch and tracks it.
Common errors
- "The current branch has no upstream branch." You ran
git pushon a branch that was never published. Rungit push -u origin <branch>. - "There is no tracking information for the current branch." You ran
git pullwith no upstream. Set one withgit branch --set-upstream-to=origin/<branch>. - A branch tracks the wrong name. If the local and remote names differ,
git pushmay refuse or push to an unexpected place, depending onpush.default. Check withgit branch -vv. - Forgetting
-uon later pushes. The flag is only needed once. Repeating it is harmless.
Summary of the roles
| Term | Where it lives | Example |
|---|---|---|
| Local branch | Your repository | `feature/login` |
| Remote-tracking branch | Your repository, updated by fetch and push | `origin/feature/login` |
| Remote branch | The server | `feature/login` on `origin` |
| Upstream | A config link from the local branch to a remote-tracking branch | `feature/login` to `origin/feature/login` |
Exercise
You created a branch fix/typo and committed on it. Which command publishes it and lets you use plain git push and git pull afterward, and how can you confirm the pairing and see whether you are ahead of the remote?
Show answer
Run git push -u origin fix/typo. The -u flag creates the branch on origin and records origin/fix/typo as the upstream. Confirm with git branch -vv, which shows [origin/fix/typo] next to the branch, plus ahead N or behind N when the counts differ from your last fetch.
Exercise
git pull on your branch prints "There is no tracking information for the current branch." The remote branch origin/feature/search already exists and you have fetched. What do you run to fix it?
Show answer
Run git branch --set-upstream-to=origin/feature/search. This pairs the current branch with that remote-tracking branch, so git pull knows what to integrate.