How to Prune Local Git Branches Safely
Prune local Git branches by checking merge ancestry, missing upstreams, and worktrees. Verify squash-merged branches before approving them for deletion.

In Git, prune local branches only after you refresh your remote-tracking refs, then audit each local branch against the base it was meant to land on. A branch marked [gone] has lost its upstream reference. A branch listed by git branch --merged origin/main has a tip reachable from origin/main. Check both facts and who is still using the branch before you delete it.
We tested five branches in a disposable Git repository. Three upstream references disappeared after a fetch. Only one of those local branches was an immediate deletion candidate. One had been squash-merged and needed its pull request checked. The third still held unmerged work. A separate branch had new commits under an old, merged pull request's name. That one stayed, too.
What does pruning actually remove?
With an ordinary fetch refspec, git fetch --prune origin updates your local view of origin and removes obsolete remote-tracking refs such as origin/topic. The local branch topic remains, along with its current tip. Git describes pruning in terms of fetch refspecs, which matters if your repository maps extra refs or tags. Check the mapping if you inherited a custom configuration.
| Name | What it refers to | How it changes |
|---|---|---|
refs/heads/topic |
Your local branch | Switch to it, commit, or explicitly move or delete it |
refs/remotes/origin/topic |
Your clone's last fetched view of the remote branch | Fetch, including prune when the remote branch disappears |
Remote refs/heads/topic |
A branch on the server | A server-side change or an accepted push |
A deleted remote branch could mean the pull request merged and GitHub removed its head. It could also mean someone closed the pull request without merging, renamed the branch, or removed a branch whose local copy had unpublished commits. A missing upstream gives you a useful list to review. You still need separate evidence about the work. See the Git fetch documentation for the behavior and the GitHub branch deletion settings for the rules and fork exceptions.
Build the list before deleting anything
Start by checking the repository and remote you intend to audit. Use the base branch for the work in question. The examples use origin/main. If your team integrates into develop or a release branch, substitute that remote-tracking ref after confirming it exists and was fetched.
git remote -v
git config --get-all remote.origin.fetch
git fetch --prune origin
git for-each-ref \
--format='%(refname:short) %(objectname) %(upstream:short) %(upstream:track)' \
refs/heads
git branch --merged origin/main
git worktree list --porcelain
Run this in one clone of the intended repository. The first two commands let you catch a wrong remote or a fetch configuration you didn't expect. The ref listing gives you branch names, full object IDs and tracking states. An empty tracking state may indicate an upstream is current, or that there is no upstream at all. Read it together with the upstream name. If fetch fails, stop and work out why before relying on the remaining output.
The merged list answers a specific graph question, against the base you supplied. A branch can be reachable from one release branch and absent from another. It can also be reachable from the current checkout while absent from the branch you meant to verify. If you need to check one item from a teammate's handoff, run the ancestry check with its full commit hash and the intended base:
git merge-base --is-ancestor <reviewed-head-commit> origin/main
Exit status 0 means the reviewed commit is an ancestor. Exit status 1 means it isn't. Other errors need investigation before acting. You can pair the exit code with the local branch tip from git rev-parse refs/heads/topic to confirm you're testing the current work, particularly if someone pushed updates while you were reviewing. The Git merge-base documentation explains the exit status.
Finally, read the worktree list. Branches can be checked out in a linked worktree even though nobody is using them in the directory you're looking at. A detached worktree has no branch name in the listing, and it may still contain important uncommitted edits or a commit nobody has pinned. Check with the owner before removing anything from that checkout. The guide to creating and removing a second Git worktree shows how to inspect the checkout itself.
What happened in the five-branch test?
In a disposable repository with a local bare remote, Git 2.53.0 removed three stale remote-tracking refs. The five local branch names all survived. We compared each tip with origin/main, read the worktree declarations and used two supplied pull request records to model the squash-merge checks.
| Local branch | Upstream after prune | Tip reachable from origin/main |
Next action |
|---|---|---|---|
merged |
Gone | Yes | Eligible. Recheck the saved tip and delete the local branch when its owner is done. |
squashed |
Gone | No | Review the merged pull request. Its recorded head matched the local tip, and the change reached the base. |
gone |
Gone | No | Keep. The tip had no merge evidence. |
reused |
Present | No | Keep. The branch had advanced beyond the head recorded by an older merged pull request. |
worktree-in-use |
None configured | Yes | Keep. It was checked out in a linked worktree. |
The gone branch is the easiest mistake to make. It looked the same as merged in the upstream column, and both names were removed from the bare remote in the fixture. The ancestry result distinguished them. The local gone ref still pointed to a commit that wasn't on origin/main.
The reused case matters when a human or an agent uses a familiar branch name for a follow-up. The older change was present on origin/main and the new file was absent. An old merged PR by itself would have missed the difference. These were real local branch and merge operations. The pull request records were fixtures. We didn't run the procedure against a hosted repository.
Review a squash-merged branch against its exact pull request head
A squash merge writes a new commit on the base. The original branch tip may remain outside the base's ancestry even though its changes landed. Treat an exact matching, merged pull request as evidence to inspect. If the branch tip has changed or the PR targeted a different base, keep the branch while you verify what moved.
- Find the pull request in GitHub or check it with the CLI. Confirm its state, head repository, base and merge commit. A closed PR and a merged PR carry different evidence.
- Compare its recorded head commit to
git rev-parse refs/heads/topic. If the hashes differ, inspect the newer work. Don't apply an older PR decision to it. - Confirm the change landed on the intended base. For a squash merge, inspect the merge commit and changed paths. If a later revert removed the change intentionally, record that outcome with the reviewer.
- Before an approved deletion where the normal Git ancestry guard won't apply, preserve the tip in a named recovery ref and verify that ref resolves to the recorded commit. Follow your team's policy for the exception.
gh pr view 123 --json state,headRefName,headRefOid,headRepository,baseRefName,mergeCommit
git rev-parse refs/heads/topic
git show --stat <verified-merge-commit>
git branch recovery/topic <verified-local-tip>
Replace the example pull request number, branch and commit placeholders with values from your audit. The Git commands can be run without switching branches. The CLI command reads a GitHub pull request and requires access to the repository. GitHub's merge reference explains what squash and rebase do to commit identity, and the GitHub CLI manual documents the supported fields.
Stacked PRs need an extra check. The child may still use the parent branch as its base after the parent lands. Review and retarget the child in the team's agreed order before taking away that base. See the stacked pull request guide for the handoff and review order.
Remove only the branches you've approved
Run deletion for a named local branch after you've checked the current tip, integration base, other worktrees and owner. Use Git's guarded delete for an ordinary merged-branch case. The relevant comparison for your audit is the base you chose. Git's own guard may use the branch's configured upstream, or the current HEAD when no upstream was configured. Those can be different checks.
git rev-parse refs/heads/topic
git branch -d -- topic
If Git refuses, trace the reason before changing any other command. A checked-out branch needs attention in the checkout that owns it. A squash-merged branch may need the reviewed exception above. Keep protected branch names and anything with open work out of the cleanup list, even when their tip is reachable. See the Git branch documentation for the deletion rules and the Git worktree documentation for the checkout listing.
The cleanup in this guide is local. Removing a local branch usually leaves the remote alone. A remote deletion affects teammates and may be governed by repository rules. Handle that separately with the owner. Likewise, rolling back a change that reached the shared branch requires a release decision. Cleaning up a local branch name changes neither what ran in production nor what needs to be rolled back.
Leave a cleanup record the next person can use
For each branch under review, write down the full inspected tip, target base, relevant PR, whether a worktree holds it, and who approved the disposition. If a recovery ref was created, include its name and commit. That record lets the next engineer or agent recheck one branch without rebuilding the history from chat. Record when the audit was done so a later branch update prompts a fresh check.
If your team uses a short-lived branch model, align cleanup with the actual trunk-based or Gitflow integration point. In either case, have the agent produce the evidence and let the owner review the list. On the next handoff, start with the branch that looks finished but still has either a checked-out worktree or a newer tip. Those are the easiest clues to miss when the branch list gets long.
Frequently asked questions
Does git fetch --prune delete local branches?
With the usual fetch refspec, pruning removes obsolete remote-tracking references in your clone. Your local working branches remain, including branches whose upstream is now marked gone. Inspect the configured fetch refspec first if your team maps other references or tags. Delete a named local branch only after its work has been checked.
How can I tell which local branches are already merged?
Fetch the correct remote, then run git branch --merged with the integration branch, such as origin/main, as the argument. The output identifies tips reachable from that base. Check linked worktrees and confirm the reviewed commit is still the current tip before deleting any name from that list.
Why is a squash-merged branch missing from git branch --merged?
A squash merge applies the changes in a new commit on the base branch. The original tip can remain outside the base's ancestry. Verify that the merged pull request's recorded head matches the current local tip, confirm the changes landed on the intended base, and preserve a reviewed recovery ref before exceptional cleanup.
Can I delete a local branch when its upstream says gone?
Treat gone as a signal to review the branch. It means the tracked remote reference is missing and tells you nothing about whether the work landed. Verify the actual branch tip against the intended base, check open or merged pull requests as appropriate, and ask who still uses the checkout.
Why won't Git delete a branch checked out in another worktree?
A linked worktree has its own checkout and can still be using the branch. Use git worktree list with the porcelain option to find the path. Inspect its status and coordinate with its owner before changing or removing the checkout. Leave it in place while the work is still active.