finder2
finder2

Reputation: 1084

Why does local git show more branches than server

The list of branches in Azure DevOps Server (2019, locally installed) correctly shows only three branches in all section cause all others have been merged and deleted:

current branches

Why does git branch -r returns plenty of the old merged or deleted branches (via UI)? And how to fix the repo to only contain the shown branches from the Azure DevOps Server web interface?

local branches

Upvotes: 1

Views: 614

Answers (1)

rickvdbosch
rickvdbosch

Reputation: 15621

When a branch is removed on the origin, local branches that track the branch are not removed. This is an explicit actions since there might still be work in there you haven't pushed yet. To clean up local branches that track a branch that no longer exists on the origin, you need to prune remote branches.

The git remote prune command removes local remote tracking branches where the branch no longer exists on the remote.

Prune
Deletes stale references associated with <name>. By default, stale remote-tracking branches under <name> are deleted, but depending on global configuration and the configuration of the remote we might even prune local tags that haven’t been pushed there. Equivalent to git fetch --prune <name>, except that no new references will be fetched.

Source: git documentation - prune

If you want to see the branches that would be removed running the prune command without actually removing them, add the --dry-run option.

Example: git remote prune origin --dry-run

EDIT:
As torek commented:

You can also use git fetch --prune (abbreviated git fetch -p) or set fetch.prune to true.
The last one means you never have to run git remote prune or git fetch -p, at the cost of not being able to restore an accidentally-deleted server branch.

Upvotes: 5

Related Questions