HOWTO · Git

How to Prune Local Branches in Git

Learn how Git pruning removes stale remote-tracking references, how to identify local branches with gone upstreams, and how to delete selected branches safely.

On this page

Git uses the word prune for removing stale remote-tracking references. That is different from deleting a local branch. If a teammate deletes feature-x from origin, your local origin/feature-x reference can remain until the next fetch with pruning. Your own local branch named feature-x remains too; review and delete it separately if it is no longer needed.

This distinction matters when cleaning up a repository: refresh the remote-tracking information, inspect which local branches report a missing upstream, and delete only the branches you have reviewed. A branch marked gone is a useful candidate, not proof that its commits are disposable. Keep or back up work you may need before removing a branch.

What Git prune removes

A remote-tracking branch such as origin/main is Git’s local record of a branch observed on a remote. It is not the remote branch itself and is not the same as a local branch such as main. git fetch --prune updates remote-tracking references and removes those whose corresponding remote branches no longer exist. It does not remove local branches, commits, or files from your working tree.

Pruning can reduce stale names in branch listings, but it does not decide whether local work is finished. A local branch may contain commits that were never pushed, or may still be useful even though its upstream was deleted. Check its history and coordinate with your team before deleting it.

Refresh remote-tracking branches

First fetch from the remote whose references you want to refresh. Replace origin with the remote name you use. The --prune option applies to the fetched remote; you can also configure pruning for fetches if you want it to happen regularly.

Run git fetch --prune origin to refresh that remote.

To see configured remotes, use git remote -v. Then inspect local branches and their upstreams with git branch -vv. A branch whose upstream was removed is marked [gone] after pruning. Without a fresh fetch, that status may be out of date. git remote prune origin is another way to remove stale remote-tracking references for origin; it still does not delete local branches.

Find and delete stale local branches

Review the output of git branch -vv and identify local branches marked [gone]. For each candidate, inspect its commits and working-tree context as needed. git branch --merged main can help identify branches whose changes are reachable from main, but use the appropriate integration branch for your repository. A gone upstream and a merged status answer different questions: the first describes tracking, while the second describes commit reachability.

Delete a reviewed branch with git branch -d feature-x. Git checks whether the branch is merged into its configured upstream when it has one; if it has no upstream, Git checks whether it is merged into the current branch. This safeguard helps protect work but does not replace your review. The separate git branch --merged main command explicitly lists branches merged into main, regardless of the current branch; substitute your integration branch as appropriate. If you have verified that the commits are preserved elsewhere and intentionally want to remove an unmerged branch, git branch -D feature-x forces deletion and can make that work harder to find. Do not apply -D merely because an upstream is gone.

The following small example shows how to recognize the marker in captured branch-list text. It only parses the sample; use Git itself to refresh and review your repository.

Run this example:

branch_status = "  feature-x  abc1234 [gone] work in progress"
parts = branch_status.split()
if "[gone]" in parts:
    print(f"Review local branch: {parts[0]}")
Review local branch: feature-x

Automate cleanup safely

Automation can save repeated inspection time, but a script should make candidate selection visible before it deletes anything. Avoid extracting names from human-formatted git branch -vv output with a whitespace-splitting pipeline: branch names can contain characters that make such parsing unreliable, and deleting every [gone] branch can discard useful work.

A cautious routine is to run git fetch --prune origin, generate a candidate list using NUL separators, review it, and confirm each deletion. The supplied runtime run exercised only the outside-repository guard and printed the message shown below; it did not exercise candidate selection or deletion in a repository. The example reads the branch name and tracking status as separate NUL-terminated fields, so it does not split names on spaces or other display formatting. It skips deletion when no branch has a gone upstream, prompts for each candidate, and uses ordinary -d; Git’s error remains visible if it refuses a deletion. Run it from the repository you intend to clean:

if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
    printf 'Run this from inside the repository to clean.\n'
    exit 0
fi

declare -a candidates=()
while IFS= read -r -d '' branch && IFS= read -r -d '' tracking; do
    branch=${branch#$'\n'}
    if [[ $tracking == *'[gone]'* ]]; then
        candidates+=("$branch")
    fi
done < <(git for-each-ref --format='%(refname:short)%00%(upstream:track)%00' refs/heads)

if ((${#candidates[@]} == 0)); then
    printf 'No local branches have a gone upstream.\n'
    exit 0
fi

for branch in "${candidates[@]}"; do
    printf 'Candidate: %q\n' "$branch"
done
for branch in "${candidates[@]}"; do
    answer=
    if [[ -r /dev/tty ]]; then
        printf 'Delete %q with git branch -d? [y/N] ' "$branch" > /dev/tty
        IFS= read -r answer < /dev/tty || answer=
    else
        printf 'No terminal available; skipping %q.\n' "$branch"
    fi
    if [[ $answer == [yY] ]]; then
        git branch -d -- "$branch"
    fi
done
Run this from inside the repository to clean.

Read the candidate list before answering any prompt. A gone upstream only identifies a branch to review; it does not establish that its commits are disposable. Do not feed an unreviewed list to git branch -D. If you prefer to choose branches yourself, use the displayed names and delete selected branches individually with git branch -d -- <branch>.

For recurring cleanup, consider setting fetch.prune for the repository or for a specific remote. This automates removal of stale remote-tracking references during fetch; it still does not remove local branches. Check the setting with git config --get fetch.prune and choose a scope that fits your repository before changing configuration.

Verify results and recover a branch

After cleanup, run git branch -vv again. The deleted local branch should no longer appear, while any other branch whose upstream is gone remains until you remove it. To confirm the remote-tracking cleanup, use git branch -r and check that the stale origin/... reference is absent. These checks show different things: git branch -r lists remote-tracking references, and git branch -vv lists local branches with tracking status.

If you deleted a local branch by mistake, git reflog may show the commit at the branch tip. When you have identified the correct commit, recreate a branch with git branch feature-x <commit>. Reflog entries can expire and are not a substitute for a backup, so recover promptly and verify the resulting branch before continuing. If the commit is already known, creating a branch at that commit is the direct recovery path.