How to find the nearest parent of a Git branch?

后端 未结 21 1643
野性不改
野性不改 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:11

    @Mark Reed: You should add that the commit line should not only contain an asterisk, but begin with an asterisk! Otherwise commit messages that contain an asterisk are also included in the matched lines. So it should be:

    git show-branch -a | awk -F'[]^~[]' '/^\*/ && !/'"$current_branch"'/ {print $2;exit}'

    or the long version:

    git show-branch -a           |
      awk '^\*'                  | # we want only lines that contain an asterisk
      awk -v "$current_branch"   | # but also don't contain the current branch
      head -n1                   | # and only the first such line
      sed 's/.*\[\(.*\)\].*/\1/' | # really, just the part of the line between []
      sed 's/[\^~].*//'            # and with any relative refs (^, ~n) removed`
    

提交回复
热议问题