Compare a local Git branch with a remote branch

Refresh remote-tracking refs, inspect commit reachability and file differences, and distinguish two-dot and three-dot comparisons.

Best practices
Illustration of Git branches and version history

Fetch first, then compare your local branch with the corresponding remote-tracking ref:

git fetch origin
git log main..origin/main --oneline
git log origin/main..main --oneline
git diff main origin/main

The first log lists commits reachable from origin/main but not main; the second lists the opposite set. The diff shows how to change the main tree into the origin/main tree. These are different questions: distinct commit histories can still produce identical file contents.

Review both sides of a divergence

git log --left-right --oneline main...origin/main
git log --graph --oneline main origin/main
git rev-list --left-right --count main...origin/main

For the left/right forms, the left side is main and the right side is origin/main. Counts measure commit reachability, not lines changed or proof that a change was never pushed anywhere. A cherry-picked equivalent patch can have a different commit ID.

Narrow a file comparison

git diff main origin/main --stat
git diff main origin/main -- src/app.js

These endpoint comparisons ignore uncommitted working-tree edits. Use ordinary git diff and git diff --cached to inspect those separately.

Expression Meaning
git log A..B Commits reachable from B but not A
git log A...B Commits reachable from either side but not both
git diff A..B Same endpoint-tree comparison as git diff A B
git diff A...B Compare a merge base of A and B with B

Three-dot diff is asymmetric. It needs an appropriate merge base; unrelated or incomplete shallow histories can prevent that comparison, and multiple merge bases require care.

Use upstream summaries

git status
git branch -vv

Ahead/behind summaries depend on configured upstreams and locally available refs. They do not query the server on every display. origin/main is the last fetched state available to this repository; it can become stale again immediately if someone pushes afterward.

Fetch normally leaves your current branch and working files unchanged, but it does have effects: it downloads objects, updates refs and FETCH_HEAD, and may update tags or prune refs according to options and configuration. Check that its refspec actually includes the branch you want.

Can I use an editor instead?

Editor and IDE Git integrations can provide branch comparison views. Confirm which two refs and direction the view uses; a “changes since branching” view can use a merge base rather than the endpoint comparison shown above.

See git-diff, git-log, and git-fetch.

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