LinuxCommandLibrary

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

$ git delete-squashed-branches
copy

Delete all branches that were "squash-merged" into a specific branch
$ git delete-squashed-branches [branch_name]
copy

SYNOPSIS

git delete-squashed-branches [-d|--dry-run] [-f|--force] [-r|--remote] [-b <base-branch>|--base=<base-branch>]

PARAMETERS

-d, --dry-run
    Shows which branches would be deleted without actually deleting them.

-f, --force
    Deletes branches without prompting for confirmation.

-r, --remote
    Also deletes the corresponding remote tracking branch (e.g., origin/feature-branch) in addition to the local one.

-b <base-branch>, --base=<base-branch>
    Specifies the base branch to check against for squashed merges. Defaults typically to main or master.

DESCRIPTION

This utility script identifies and removes local Git branches that have been squashed and merged into a specified base branch (e.g., main or master). It's particularly useful after a common Git workflow where feature branches are squashed into a single commit before being merged, leaving no direct merge history for the feature branch.

The script helps clean up stale local branches, distinguishing them from branches that were fully merged with a merge commit, or those that still contain unique unmerged work. It often includes a dry-run option to preview deletions and a confirmation prompt for safety. This unofficial command streamlines repository management by automatically clearing branches whose work has been incorporated into the main development line, even when the original branch history isn't preserved through a traditional merge.

CAVEATS

Not a standard Git command: This is a community-driven script, so its exact behavior and options may vary depending on the specific implementation used.
Reliance on history: The script relies on Git's commit history. If history has been re-written (e.g., with git rebase -i), the script might not accurately identify squashed branches or could potentially delete branches that still contain unique work.
False positives/negatives: While designed to be smart, it's possible for the script to misidentify branches as squashed, or miss truly squashed branches, especially in complex or unusual repository states. Always use the --dry-run option first.
Irreversible deletion: Deleting branches is generally irreversible without manual intervention or recovery from backups.

HOW IT WORKS (COMMON LOGIC)

Typically, the script identifies branches whose commits are fully contained within the history of the base branch, even if there isn't a direct merge commit (which is characteristic of a squash merge). It might do this by checking if git log <feature-branch> ^<base-branch> shows no unique commits, or by using other more complex Git plumbing commands to verify ancestry and content. It usually excludes the currently checked-out branch and branches configured as protected.

HISTORY

This is not an official Git command with a formal history in the Git project. Instead, git-delete-squashed-branches refers to a common pattern of community-developed shell scripts or aliases. These scripts emerged from the need to manage local branches effectively in workflows that frequently use squash merges (e.g., GitHub's 'Squash and merge' feature). Developers shared and refined these scripts in personal dotfiles, GitHub gists, and Stack Overflow answers, making it a widely adopted unofficial tool for Git cleanup.

SEE ALSO

git branch(1), git merge(1), git log(1), git remote(1)

Copied to clipboard