How can I list all branches that are commit ancestors?

I want to see all branches that are commit ancestors abcdef1234.

This is a kind of inversion:

git branch --contains abcdef1234

The command above will list all branches that are descendants abcdef1234. I want to see a list of all branches that are ancestors abcdef1234.

I am also interested in the equivalent for tags.

UPDATE

To be more clear, I want to say that I want to see a list of all commits that meet 2 criteria:

  • They are the ancestors abcdef1234
  • Currently, they are indicated by (local or remote) branches.

Obviously, most commits at some point had a branch pointing to them when they were completely new. My only concern is whether they are branches at this particular moment.

+4
3

git branch --merged abcdef1234 , . , commit (, , ), , , .

+3

. ( , , .)

K (abcdefg1234 ). L, L refs/heads/* refs/remotes/* ( ). L C. C K, L.

[ : sschuberth , git branch --merged; , , , git for-each-ref --merged, , script , , , , git branch . , . git tag --merged, . Git , script.:-)]

script (untested!), . , git for-each-ref Git, , 1.6-ish - , 1.7-ish; , git merge-base --is-ancestor.

#! /bin/sh

# find branches and remote-tracking branches that targt
# ancestors of $1.
showthem() {
    local tgt label lbltgt
    tgt=$(git rev-parse "$1") || return $?
    git for-each-ref --format='%(refname:short) %(objectname)' \
            refs/heads refs/remotes |
        while read label lbltgt; do
            if git merge-base --is-ancestor $lbltgt $tgt; then
                echo "$label"
            fi
        done
    return 0
}

case $# in
0) usage 1>&2; exit 1;;
1) showthem "$1";;
*) for i do "echo ${i}:"; showthem "$i" || exit; done
esac

(, , , , origin/HEAD.)

+2
comm -23 <(git branch -a | sort) <(git branch -a --contains abcdefg1234 | sort)

This will give you all branches that do not have commit abcdefg1234; it outputs git branch -aminus output git branch -a --contains abcdefg1234.

0
source

All Articles