LinuxCommandLibrary

git-show-unmerged-branches

Show branches that have unmerged changes

TLDR

Print all branches which are not merged into the current HEAD

$ git show-unmerged-branches
copy

SYNOPSIS

git-show-unmerged-branches [<options>]

PARAMETERS

-r, --remote
    Show unmerged branches on remote repositories.

-t <target_branch>, --target=<target_branch>
    Specify the target branch to compare against (defaults to 'main').

-d, --delete
    Delete the unmerged branches interactively (use with caution!).

-h, --help
    Display help message.

DESCRIPTION

This command is not a standard Git command. It's typically a custom script or alias designed to identify Git branches that have not been merged into a specific target branch (usually 'master' or 'main'). It automates the process of finding branches that may contain unintegrated work. The script usually iterates through all local or remote branches, compares them to the specified target branch (e.g., 'main'), and lists those that have commits not present in the target. This helps developers identify branches requiring attention - either they should be merged, discarded, or rebased. Without this tool or equivalent, developers would need to manually compare each branch against the target branch using `git log` or `git diff`, which can be tedious and error-prone, especially in repositories with many branches. This tool is a helper in managing branch integrity and avoiding accidentally losing important work. The command usually relies on common `git` tools, like `git for-each-ref`, `git branch`, `git rev-list`, `git merge-base`, and `git remote`.

CAVEATS

Since this is not a standard Git command, its behavior may vary depending on the specific script or alias implementation. It's important to understand the underlying logic of the script before using it, especially the `-d` option. Backups are always recommended before performing destructive operations.

EXAMPLES

1. Show unmerged local branches compared to 'main':
git-show-unmerged-branches

2. Show unmerged branches compared to 'develop':
git-show-unmerged-branches -t develop

3. Show unmerged branches on remote 'origin':
git-show-unmerged-branches -r

IMPLEMENTATION NOTES

A typical implementation often involves the following steps:
1. Fetch all remote branches (if `-r` option is used).
2. Iterate through all branches (local or remote).
3. Determine the merge base between the current branch and the target branch using `git merge-base`.
4. List branches where `git rev-list ..` returns a non-empty result, indicating commits present in the branch but not in the target.
5. For interactive deletion (`-d` option), prompt the user for confirmation before deleting each branch.

SEE ALSO

git branch(1), git log(1), git merge(1), git remote(1), git rev-list(1), git for-each-ref(1)

Copied to clipboard