How to find the nearest parent of a Git branch?

后端 未结 21 1711
野性不改
野性不改 2020-11-22 00:54

Let\'s say I have the following local repository with a commit tree like this:

master --> a
            \\
             \\
      develop c --> d
               


        
21条回答
  •  梦如初夏
    2020-11-22 01:14

    Since none of the answers above worked on our repository, I want to share my own way, using latest merges in git log:

    #!/bin/bash
    git log --oneline --merges "$@" | grep into | sed 's/.* into //g' | uniq --count | head -n 10
    

    Put it in a script named git-last-merges, which also accepts a branch name as argument (instead of current branch) as well as other git log arguments

    From the output, we can manually detect the parent branch(es) based on own branching conventions and number of merges from each branch.

    EDIT: If you use git rebase on child branches often (and merges are fast-forwarded often so there aren't too many merge commits), this answer won't work well, so I wrote a script to count ahead commits (normal and merge), and behind commits (there shouldn't be any behind merge in parent branch) on all branches comparing to the current branch. Just run this script and let me know if works for you or not

    #!/bin/bash
    HEAD="`git rev-parse --abbrev-ref HEAD`"
    echo "Comparing to $HEAD"
    printf "%12s  %12s   %10s     %s\n" "Behind" "BehindMerge" "Ahead" "Branch"
    git branch | grep -v '^*' | sed 's/^\* //g' | while read branch ; do
        ahead_merge_count=`git log --oneline --merges $branch ^$HEAD | wc -l`
        if [[ $ahead_merge_count != 0 ]] ; then
            continue
        fi
        ahead_count=`git log --oneline --no-merges $branch ^$HEAD | wc -l`
        behind_count=`git log --oneline --no-merges ^$branch $HEAD | wc -l`
        behind_merge_count=`git log --oneline --merges ^$branch $HEAD | wc -l`
        behind="-$behind_count"
        behind_merge="-M$behind_merge_count"
        ahead="+$ahead_count"
        printf "%12s  %12s   %10s     %s\n" "$behind" "$behind_merge" "$ahead" "$branch"
    done | sort -n
    

提交回复
热议问题