git-delete-squashed-branches
Delete local branches already merged via squash
TLDR
Delete all branches that were "squash-merged" into the current checked out branch
Delete all branches that were "squash-merged" into a specific branch
SYNOPSIS
git delete-squashed-branches [-r|--remote-only] [target-branch]
PARAMETERS
-r, --remote-only
Skip local branches; delete only matching remote branches.
target-branch
Branch to check merges against (default: master). Positional argument.
DESCRIPTION
The git-delete-squashed-branches command is a custom Bash script designed to clean up Git repositories by deleting local and/or remote branches that have been squash-merged into a specified target branch, typically master or main.
Squash merges combine multiple commits into one without preserving the original branch history as a merge commit, making standard git branch -d checks ineffective. This script detects such branches by verifying if all commits from a candidate branch are present in the target branch (using git rev-list), while ensuring the branch tip is not a direct ancestor (ruling out regular merges).
It lists matching branches interactively, prompting for confirmation (y/n) before deletion. Local branches are pruned with git branch -D, and remote ones via git push origin --delete. Ideal for teams using squash-merge workflows to reduce clutter without risking unmerged work.
Note: Requires Git 1.8+ for reliable merge-base checks; performance may lag on repos with large histories due to commit listing.
CAVEATS
Third-party script, not official Git tool; test on copies first. Interactive prompts prevent accidents but can be tedious. Fails on complex rebases/shared histories; assumes linear squash. No support for protected branches—review output. Slow on repos with 10k+ commits.
DETECTION LOGIC
Checks: git merge-base --is-ancestor branch target (fails for squash) + all branch commits exist in target rev-list.
INSTALLATION
Save script, make executable: chmod +x git-delete-squashed-branches. Add Git alias: git config --global alias.dsb '!path/to/script'.
HISTORY
Created by Jonathan Nicol in 2013 as GitHub Gist #5539699. Gained popularity (~1k stars) for squash-merge cleanup. Minor updates for Git versions; community forks add features like non-interactive mode.
SEE ALSO
git-branch(1), git-merge(1), git-push(1), git-rev-list(1)


