git-show-merged-branches
Show branches already merged into current branch
TLDR
Print all branches which are merged into the current head
SYNOPSIS
git show-merged-branches [target_branch] [options]
Note: As this is a user-defined script, the exact syntax and available options are implementation-dependent.
PARAMETERS
[target_branch]
Optional. The name of the branch to check against. If omitted, the command typically defaults to the current HEAD branch.
--remote or -r
Optional. Includes remote-tracking branches in the output, showing which remote branches have been merged into the specified target_branch.
--dry-run or -n
Optional. When the script includes deletion capabilities, this option shows which branches would be deleted without actually performing the deletion.
--delete or -d
Optional. Some advanced implementations include this option to automatically delete the identified merged branches. Use with caution.
DESCRIPTION
The git-show-merged-branches command is not a built-in Git command but rather a commonly found user-defined script or Git alias. Its primary purpose is to identify and display local or remote branches that have already been merged into a specified target branch (by default, the current HEAD). This utility is invaluable for repository cleanup, as it helps developers quickly pinpoint branches that are safe to delete because their changes have been integrated into the main development line.
While its exact implementation can vary, it typically leverages Git's native git branch --merged functionality, often combined with filtering to exclude protected branches like main, master, or develop. Its widespread use highlights a common need within Git workflows to maintain a clean and manageable branch history, preventing clutter from stale, already-integrated feature or hotfix branches.
CAVEATS
This command is not a standard Git command distributed with Git itself. Its existence, behavior, and available options depend entirely on whether a user or system administrator has created it as a custom script or Git alias. Therefore, it may not be available on all systems or in all Git environments, and its output or functionality might differ significantly between implementations. Users should verify its presence and examine its source code if in doubt.
TYPICAL IMPLEMENTATION
A common way to implement git-show-merged-branches involves piping the output of git branch --merged through grep to filter out the current branch and protected branches (like main, master, develop). For remote branches, git branch -r --merged might be used. An example alias definition could look like:
git config --global alias.sbm '!git branch --merged | egrep -v "(^\*|main|master|develop)"'
More sophisticated scripts might include options for deleting branches after listing them, often incorporating xargs git branch -d or git push origin --delete for remote branches.
HISTORY
The concept behind git-show-merged-branches emerged organically within the Git community as a response to the need for efficient branch management and repository hygiene. As Git repositories grow and feature branches proliferate, the manual tracking of merged branches becomes tedious. Developers frequently shared personal scripts and aliases to automate this cleanup, leading to a de facto standard functionality, even without a formal inclusion in the Git core. Its evolution is thus a testament to community-driven problem-solving in the Git ecosystem, with various slightly different implementations addressing the same core problem of identifying stale, merged branches.
SEE ALSO
git-branch(1): For listing, creating, and deleting branches. The underlying mechanism for identifying merged branches., git-merge(1): For integrating changes from one branch into another, which is the prerequisite for a branch to be considered merged., git-config(1): For configuring Git aliases, which is a common way this command is defined (e.g., git config --global alias.show-merged-branches '!f() { git branch --merged "${1:-HEAD}" | grep -v "\*|main|master|develop" ...; }; f').