How to clean up stale Git branches (local & remote) in minutes

Over time, branches accumulate, they get merged, abandoned, or deleted remotely but still present locally.

Short on time? Here’s the TL;DR:

git fetch --prune          # Sync + remove deleted remote branches
git branch -vv             # Find stale branches ([gone])
git branch -d branch_name  # Delete them safely

The problem: Stale branches

Typical clutter:

1. Identify stale branches

git branch -vv
# Output example:
#   main                          abc1234 [origin/main] Latest commit message
#   feature/login                 def5678 [origin/feature/login] Add auth
#   bugfix/old-issue              ghi9012 [origin/bugfix/old-issue: gone] Fix typo

Look for [gone] → the remote branch was deleted, but the local branch still exists.

2. Delete local branches

Safe delete (merged only)

git branch -d branch_name

Fails if not fully merged (safe by default). You must be on another branch, otherwise, you get the # ERROR: can't delete currently checked out branch error.

Multiple branches

git branch -d feature/login feature/signup
# Delete several branches in one command

By pattern

git branch | grep "feature/" | xargs git branch -d
# Deletes all branches starting with "feature/"
# Test with 'git branch | grep "feature/"' first to verify which branches match

Force delete

git branch -D branch_name

Deletes regardless of merge status. Use carefully.

3. Delete remote branches

git push origin --delete branch_name
# Removes the branch from the remote repository
# Other team members will see the branch is gone, but still have it locally

Multiple branches

git push origin --delete feature/login feature/signup

4. Sync with remote (Remove “[gone]”)

git fetch --prune origin

Fetches updates and removes references to deleted remote branches. Your local branches remain, delete them manually with git branch -d branch_name.

5. Full cleanup workflow

# Step 1: Sync with remote and remove [gone] remote branches
git fetch --prune

# Step 2: List branches with tracking info to see what's left
git branch -vv

# Step 3: Delete any remaining stale local branches
git branch -d bugfix/old-issue feature/abandoned

Pro tips

comments powered by Disqus