Skip to content
Versora

Manual

Hygiene and workflow

Branch cleanup

10 min read

On this page

After a few weeks of merge requests, the LOCAL section fills up with branches that have already made it into the main branch. git branch --merged only sees some of them: when the server merges by squash or by rebase, the branch's commits are replaced by others and git no longer recognizes that the work is already there. Versora's cleanup combines three criteria, checks each branch's content and shows you the list before deleting anything.

Clean up merged branches

The Clean up branches window analyzes the repository's branches against a target branch and sets aside the ones that can go, each with its reason. Local branches come already checked. You review them, uncheck what you want to keep and delete them all at once.

Where to find it

  • Menu: right-click the LOCAL header in the left panel › Clean up branches…
  • Menu: right-click the LOCAL header › Fetch --prune and clean up branches… (see the section Fetch --prune before cleaning up)
  • Palette (Ctrl+P): "Clean up merged branches…"
  • In the Branches review: Clean up merged…, in the bar of checked branches or in a row's menu.
  • In the notice that appears after a fetch: Review… (see Suggestion after a fetch).

Step by step

  1. Open the window through one of the paths above. While it works, "Analyzing branches…" appears.
  2. Check the Target at the top. On "automatic", Versora compares with develop when the repository uses Gitflow and, in other cases, with the remote's default branch (the remote's HEAD, the default branch reported by GitLab or GitHub, main or master). The "Comparing with …" line shows the refs used, for example origin/main and main.
  3. Read the groups. Each branch appears in one of them:
    • Merged into main (the name changes with the target): the branch's commit is already in the target.
    • Merged by squash or rebase: the branch's merge request was merged and the branch's content is already in the target.
    • Upstream gone: the branch was deleted from the remote and its content is already in the target.
  4. Uncheck the branches you want to keep. The box in each group's title checks or unchecks the whole group.
  5. If you want to know why a branch was left out, open the collapsed "N branches not offered" list at the end of the window. Each one shows its reason.
  6. Click Delete N branches…. The confirmation shows how many branches go and from where.
  7. Click Delete N branches to confirm. The bar shows "Deleting local branches…" and the count ("3 of 8"); Cancel stops before the next batch.
  8. At the end, the Cleanup finished window sums up what was removed and offers Undo local deletion and Analyze again.

What each row shows

IndicatorMeaning
merged, squash/rebase or upstream goneThe reason the branch is a candidate.
content checkedVersora checked that nothing on the branch is lost. Hover over it: "Content checked: every commit on the branch has an equivalent in the target." (rebase, cherry-pick) or "Content checked: merging the branch wouldn't change anything in the target." (squash).
upstream goneThe remote branch it tracked no longer exists ("origin/feature/login no longer exists on the remote").
Merge request link (e.g. !42 or #42)"Open the merge request in the browser". Appears on branches recognized through the merged MR.

What never makes the list

Versora never offers, under any circumstances:

Reason shownSituation
"current branch"The checked-out branch.
"main line branch"The default branch, develop, main and master.
"protected (pattern)"Branches that match a protected pattern, for example release/*.
"in use by the worktree path"The branch is checked out in another worktree.
"release/hotfix not yet in main"A release/* or hotfix/* that hasn't reached production yet.
"has commits after the merge request"The MR was merged, but the branch got commits after it.
"merge request merged, but the branch's content isn't in main"The server says there was a merge, but part of the branch's content isn't in the target.
"upstream gone, but it has commits that aren't in main"The remote branch is gone, but there's work that only exists here.
"couldn't check the content (requires Git 2.38 or newer)"With an old git, Versora can't prove nothing is lost and doesn't take the risk.

The list of protected patterns is the one in Preferences › Checks ("Protected branches for this repository"), plus GitLab's or GitHub's when the server responds, and the team and company rules. See Protected branch.

OptionWhat it doesDefault
TargetBranch the others are compared with.automatic
RemoteRemote used in the comparison and in the remote cleanup.origin (or the first remote)
Include branches on originAlso analyzes the remote's branches. See Clean up on the remote too.off
Fetch --prune"Fetches origin with --prune and analyzes again".—
Suggest a cleanup after a fetch when there are at least N candidatesTurns on the notice described in Suggestion after a fetch. Applies to this repository. The number goes from 1 to 99.on, 5
Always fetch --prune when openingEvery time the window opens, runs fetch --prune before analyzing. Applies to all repositories.off

How Versora recognizes squash and rebase

For the Merged by squash or rebase group, Versora asks GitLab or GitHub for the merged merge requests (pull requests, on GitHub) of each branch. This requires the server registered in Git servers with a token. Even with the MR merged, the branch only makes the list if its content is in the target:

  • git cherry finds an equivalent commit for each commit on the branch (rebase, cherry-pick); or
  • git merge-tree shows that merging the branch wouldn't change anything in the target (squash).

Without the integration, the window warns: "To recognize squash and rebase merges through merge requests, the remote must be a GitLab or GitHub registered in Git servers. Without that, only what Git can see shows up, and only the local list of protected branches applies." The Git servers… button opens the registration. See Git servers.

What Versora does in git

The analysis only reads the repository:

bash
# which branches are already contained in the target (the git branch --merged criterion)
git for-each-ref --format=%(refname) --merged origin/main refs/heads/ refs/remotes/origin/
# content check of the squash/rebase and upstream-gone candidates
git cherry origin/main feature/login
git merge-tree --write-tree origin/main feature/login

The local deletion checks, branch by branch, that it's still on the analyzed commit and then runs:

bash
git branch -D feature/login

A branch that changed between the analysis and the deletion isn't deleted ("feature/login changed since the analysis and wasn't deleted. Analyze again.").

How to undo

Ctrl+Z undoes the whole local deletion at once, now or later: the branches come back on the same commit, with their configuration (the upstream, for example). The Cleanup finished window also has the Undo local deletion button. The entry appears in the timeline as "Branch cleanup".

The deletion on the remote can't be undone by Versora (see below).

Caveats and messages

  • "Nothing to clean up: no branch is merged into origin/main yet.": there are no candidates with that target. Try another target or do a fetch.
  • "There's no target branch to compare with (no main or master). Pick a target.": the repository doesn't have a recognizable default branch. Choose the target in the picker.
  • "No token for gitlab.exemplo.com: branches merged by squash or rebase only show up once their upstream is gone, and only the local list of protected branches applies.": register a token in Git servers so Versora can query the merge requests.
  • "Only the first N branches were checked on the server; for the rest, a squash-merged merge request isn't recognized.": in repositories with many branches, the server query has a cap. The others only appear through git's criteria.
  • "Couldn't read the protected branches from the server (…); the local list from Checks was used.": the server didn't respond in time. The local list keeps protecting.
  • "The active tab changed. Go back to the repository's tab to continue.": the window belongs to the repository it was opened in.
  • "not run (another operation was in progress)": a fetch or another operation was keeping the repository busy. The analysis runs anyway; the fetch and the deletion wait for the tab to be free.

Clean up on the remote too

The same criteria apply to the server's branches. Deleting on the remote removes the branch for everyone, so Versora never checks these branches on its own.

Where to find it

  • In the Clean up branches window: check Include branches on origin.
  • Menu: right-click a remote folder in the left panel, or a remote branch › Clean up merged branches on origin… (opens the window with the remote already included).

Step by step

  1. Open the window with the remote included. The On the remote origin group appears, with the warning "Deleting here removes the branch from the server, for everyone. Select them one by one."
  2. Check, one by one, the remote branches that should go. The bottom bar shows how many are checked in each place ("3 local branches selected", "2 on origin").
  3. Click Delete N branches…. The confirmation separates "N local branches" from "N branches on the remote origin" and runs the Push checks ("Running the push checks…"): deleting a remote branch is handled with the same care as a force push.
  4. If the checks point something out, read the findings. Back returns to the list; Delete anyway goes ahead.
  5. The bar shows "Deleting from the remote origin…" and the count.

What Versora does in git

bash
# batches of up to 40 branches per connection; each one only goes if it's still on the analyzed commit
git push --porcelain --progress \
  --force-with-lease=refs/heads/feature/login:<analyzed-sha> \
  origin :refs/heads/feature/login

--force-with-lease ensures that, if someone pushed commits to the branch after your last fetch, the server refuses and the branch stays.

How to undo

There's no undo. The confirmation warns: "They're removed from the server for everyone and can't be undone by Versora. If someone pushed commits after the last fetch, the server refuses and the branch stays." In the final summary, the line "The deletion on the remote can't be undone by Versora." reminds you of this. If you delete a remote branch by mistake and still have the local copy, push it again.

Caveats and messages

  • "The branch on the remote changed after the last fetch (someone pushed commits or already deleted it). It was left alone; fetch and analyze again.": the --force-with-lease protection worked. Fetch and analyze again.
  • "No merged branches on origin.": the remote's group is empty.

Caution

Before deleting branches from a shared remote, agree on it with the team. A branch deleted on the server disappears from everyone's panel on the next fetch.

Fetch --prune before cleaning up

The criteria depend on what your repository knows about the server. A fetch --prune brings in new branches and removes the refs of branches that have already been deleted on the server, which reveals the candidates in the Upstream gone group.

Where to find it

  • Menu: right-click the LOCAL header › Fetch --prune and clean up branches…
  • Palette (Ctrl+P): "Fetch --prune and clean up branches…" ("Updates the remote branches before analyzing"). It's unavailable with the reason "the repository has no remote" when there's no remote.
  • Inside the window: Fetch --prune button.
  • Always: check Always fetch --prune when opening.

What Versora does in git

bash
git fetch --prune origin

While it runs, the window shows "Fetching origin with --prune…" and then analyzes again.

Suggestion after a fetch

After a fetch (manual or automatic), a pull or a push, Versora runs a light analysis, with git only (without querying merge requests) and only of the local branches. If there are at least the configured number of candidates, a discreet notice appears: "5 merged branches can be cleaned up." Protected branches, including the server's, don't count.

  • Review… opens the Clean up branches window.
  • Dismiss closes the notice.

The notice appears only once for the same set of branches: if all the candidates were already suggested before, it doesn't come back. To turn it off in this repository, uncheck Suggest a cleanup after a fetch when there are at least N candidates in the window itself.

  • Preferences › Checks: the "Protected branches for this repository" list and the GitLab/GitHub query. See Protected branch.
  • Git servers: the token that makes it possible to recognize squash and rebase through the merge request.
  • The company policy (protected_branches) and the team's .versora.yml (branches.protected) add protected patterns that the cleanup respects. See Policy keys and The .versora.yml file.

For IT

A branch protected by the company or by the team never enters the cleanup, even if the person has emptied the local list of protected branches.

See also