Clean up stale Git remote-tracking branches

Prune obsolete remote-tracking refs, inspect local branches whose upstream is gone, and delete only history you have verified is no longer needed.

Best practices
Illustration of Git branches and version history

Use git fetch --prune origin to remove obsolete remote-tracking refs for origin under the normal branch refspec. Local branches such as feature-x are separate refs and remain until you delete them explicitly.

Prune the remote-tracking refs

git fetch --prune origin
# Preview a prune without fetching new objects:
git remote prune --dry-run origin
# Apply that prune:
git remote prune origin

To enable pruning for future fetches in this repository:

git config --local fetch.prune true

Use --global only if you intentionally want the same default in other repositories. Pruning follows the configured refspecs: explicit tag or other destination mappings can broaden what is removed. Inspect custom mappings before assuming the operation affects only origin/*.

Inspect local branches with missing upstreams

git branch -vv
git for-each-ref \
  --format='%(refname:short) %(upstream:short) %(upstream:track)' \
  refs/heads/

A [gone] indication means the configured upstream ref is missing locally. After a successful fetch, that often reflects a deleted remote branch, but it does not establish that all local work was integrated elsewhere.

Avoid piping the first word of git branch -vv directly into deletion commands. Its display includes markers for checked-out branches and worktrees, and a missing upstream is only a review signal.

Verify and delete one branch

git log --oneline main..feature-x
git branch --merged main
git branch -d feature-x

Check against the intended integration target, using an updated remote-tracking ref if appropriate. -d checks the branch's upstream when available, or HEAD when no upstream is set; an unresolved configured upstream can cause deletion to be refused. It is not a universal guarantee that a branch is merged into main.

If Git refuses deletion, inspect the history. Preserve needed commits with another branch or tag. Use -D only after confirming that removing the ref is intentional; it bypasses the normal merged-history check. A branch checked out in another worktree needs separate attention.

Keep cleanup manageable

A hosting service may offer automatic deletion of merged pull-request branches. That removes the remote branch, not every developer's local copy. Pair it with regular fetch/prune and deliberate local review. Pruning compares advertised refs with local mappings; its cost depends on the remote and repository, so avoid promises of zero overhead.

See git-fetch pruning, git-remote, and git-branch.

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