Find the upstream branch of a local Git branch

Use git branch -vv and upstream references to inspect tracking relationships, refresh ahead/behind counts, and change a branch upstream.

Best practices
Illustration of Git branches and version history

Run git branch -vv to see the upstream configured for each local branch. To print only the current branch's upstream, use git rev-parse --abbrev-ref '@{u}'.

Inspect all local branches

git branch -vv

For example:

* main        7f9c2d1 [origin/main] Update README
  feature-x   8a9b1c2 [origin/feature-x: ahead 2] Add feature
  local-only  c3d4e5f Local experiment

The brackets identify the upstream and any ahead/behind counts. A branch with no upstream normally has no such bracketed section. An upstream can also be another local branch; it does not have to be a remote-tracking branch.

These counts compare locally available commits. They are not a live query of the server. Refresh the remote-tracking references before interpreting them:

git fetch origin
git branch -vv

Inspect the current branch

git rev-parse --abbrev-ref '@{u}'
git status -sb
git remote show origin

The first command fails if no upstream is configured or you are in detached HEAD state. git status -sb includes branch tracking information when available. git remote show origin provides a broader view of the remote's pull and push configuration and normally contacts the server.

An upstream describes the branch used by operations such as a default pull. Push settings can send changes elsewhere, so do not assume the upstream is always the push destination.

Set, change, or remove the upstream

# Use an existing remote-tracking branch.
git branch --set-upstream-to=origin/feature-x feature-x

# Alternatively, push and record tracking after a successful push.
git push -u origin feature-x

# Remove the current branch's upstream setting.
git branch --unset-upstream

The first command changes configuration without pushing commits.

FAQ

What does @{u} mean?

It is shorthand for @{upstream} in Git revision syntax. Quote it to avoid shell interpretation. git diff '@{u}' HEAD shows the difference from the upstream snapshot to your current commit; git log '@{u}..HEAD' lists commits reachable only from your side.

Why do ahead/behind counts look stale?

Run git fetch for the appropriate remote. Tracking references describe what your clone last learned about that remote.

Why did checking out a branch set its upstream automatically?

With the default guessing behavior, git switch feature-x can create a local tracking branch when only one remote has that name and no local branch exists. Configuration, including checkout.defaultRemote, can affect how Git resolves multiple matches. The older git checkout feature-x supports similar behavior.

See git-branch, git-rev-parse, git-status, and git-remote.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting