Is it possible to filter out merged branches in git for-each-ref?

git

Solution

`git for-each-ref` and the similar `git show-ref` do not have the ability to decide whether one reference is an ancestor of another on its own, but you can always build a pipeline, e.g.:

uninteresting=master # or some other way to identify ones to discard
git for-each-ref |
while read sha1 reftype refname; do
    if git merge-base --is-ancestor $refname $uninteresting; then
        # $refname / $sha1 "is merged", or is exactly equal
        ... do whatever you like here ...
    else
        # there is no ancestry path from $uninteresting back to $refname
        ... do whatever else you like here ...
    fi
done

Note that `git merge-base --is-ancestor master master` is always true. If you don't want "equal but name differs" considered to be "merged" (and does this depend on whether it's a branch or some other ref type?) you will need some extra code here, but I suspect you do want "equal means merged".

Problem

Basically I want something similar to `git branch -a --no-merged` but I'd also like to list tags and I don't need the spaces before or stuff like `remotes/origin/HEAD -> origin/master` Can I do this with for-each-ref or is it better to just strip the stuff I don't want from `git branch` then get the tags separately?

Original source