The knowledge base / All cheat sheets
● Always in your toolkitAbout the project ↗

Git branch and upstream tracking inspection cheat sheet

Inspect the current branch, upstream tracking and local commit differences before synchronizing a repository.

Git in a trusted local repository. Remote-tracking information reflects the last completed fetch.

Updated · Git · 6 min read

CommandWhat it does
git status --short --branchInspect the branch and worktree
git branch -vvInspect local branch tracking
git branch -rList local remote-tracking references
git rev-parse --abbrev-ref --symbolic-full-name '@{upstream}'Identify the current branch upstream
git rev-list --left-right --count 'HEAD...@{upstream}'Count local and upstream-only commits

Walkthrough

Run in the intended checkout. These commands are read-only local inspections and do not fetch, merge, reset or push.

Step 1: Inspect the branch and worktree

git status --short --branch

Read the branch header and file status before planning synchronization. Ahead and behind values depend on the locally stored upstream reference, which may be stale.

Step 2: Inspect local branch tracking

git branch -vv

Compare each local branch with its configured upstream and most recent commit. Missing upstream configuration is different from an upstream branch having no new commits.

Step 3: List local remote-tracking references

git branch -r

Review the remote-tracking names known to this checkout. This command does not contact the server and can show stale references until a separately authorized fetch or prune.

Step 4: Identify the current branch upstream

git rev-parse --abbrev-ref --symbolic-full-name '@{upstream}'

This resolves the configured upstream reference; it can fail for a detached HEAD or a branch without upstream tracking. The single quotes are accepted by PowerShell and POSIX shells.

Step 5: Count local and upstream-only commits

git rev-list --left-right --count 'HEAD...@{upstream}'

The two counts compare the local references only; fetch separately before using them as a current server comparison. Quoting protects the upstream syntax in PowerShell.

WORKED INCIDENT

Practical case: A branch appears ahead and behind

Fictional teaching scenario; observations are examples, not captured production output.

Situation

A developer sees unexpected divergence from the tracked branch before publishing.

Investigation sequence

1. Inspect the branch and worktree

git status --short --branch

Record branch status and any uncommitted work before choosing a synchronization strategy.

2. Inspect local branch tracking

git branch -vv

Confirm the intended upstream configured for the current branch.

3. Identify the current branch upstream

git rev-parse --abbrev-ref --symbolic-full-name '@{upstream}'

Resolve the upstream reference explicitly using the correct shell quoting.

4. Count local and upstream-only commits

git rev-list --left-right --count 'HEAD...@{upstream}'

Compare local and upstream-only commit counts, remembering that the upstream reference can be stale.

Example findings

Illustrative findings: the current branch tracks origin/main and the locally stored references each contain commits absent from the other.

Interpret the findings

The local references have diverged in this example. This does not show current server state until the upstream reference is refreshed, and it does not prescribe a merge or rebase.

Verify the outcome

After an authorized fetch and an agreed integration method, inspect the resulting history and ensure the intended changes are preserved.

Escalation handoff

Provide branch name, upstream and commit identifiers to the repository owner without rewriting or discarding work.

Primary references

Documented reference; examples above are teaching scenarios. Confirm behavior against your deployed version.