How to list branches that contain an equivalent commit

后端 未结 4 2086
予麋鹿
予麋鹿 2020-11-30 02:28

In a prior question someone provided an answer for finding branches that contained an EXACT commit:

How to list branches that contain a given commit

The acce

4条回答
  •  眼角桃花
    2020-11-30 03:05

    The following seems to work (but hasn't been tested much). It runs git cherry for each local git branch, and prints the branch name if git cherry doesn't list the commit as missing from the branch.

    # USAGE: git-cherry-contains  [refs]
    # Prints each local branch containing an equivalent commit.
    git-cherry-contains() {
        local sha; sha=$(git rev-parse --verify "$1") || return 1
        local refs; refs=${2:-refs/heads/}
        local branch
        while IFS= read -r branch; do
            if ! git cherry "$branch" "$sha" "$sha^" | grep -qE "^\+ $sha"; then
                echo "$branch"
            fi
        done < <(git for-each-ref --format='%(refname:short)' $refs)
    }
    

    See Andrew C's post for a great explanation of how git cherry actually works (using git patch-id).

提交回复
热议问题