At an interactive prompt, I can use the git branch -r --no-merged
command to determine remote branches that haven't been merged yet. However, git branch
is a porcelain command and therefore is not suitable for use in a script.
Is there any good plumbing command I can use to determine the same branches returned by the --no-merged
option?
I found that the command git merge-base
supports an --is-ancestor
argument to check if a commit is contained in the history of another. Using this, I can find unmerged remote refs like this (in ksh):
function showUnmerged {
typeset -a unmergedRefs=()
git for-each-ref --format='%(objectname) %(refname)' refs/remotes | \
while read -r hash ref; do
git merge-base --is-ancestor "$hash" "$1" || unmergedRefs+=("$ref")
done
echo "${unmergedRefs[@]}"
}
showUnmerged HEAD
This doesn't seem to be particularly efficient, but it does work and as far as I know, both for-each-ref
and merge-base
are plumbing commands