How to find the nearest parent of a Git branch?

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

    git parent

    You can just run the command

    git parent

    to find the parent of the branch, if you add the @Joe Chrysler's answer as a git alias. It will simplify the usage.

    Open gitconfig file located at "~/.gitconfig" by using any text editor. ( For linux). And for Windows the ".gitconfig" path is generally located at c:\users\your-user\.gitconfig

    vim  ~/.gitconfig
    

    Add the following alias command in the file:

    [alias]
                parent = "!git show-branch | grep '*' | grep -v \"$(git rev-parse --abbrev-ref HEAD)\" | head -n1 | sed 's/.*\\[\\(.*\\)\\].*/\\1/' | sed 's/[\\^~].*//' #"
    

    Save and exit the editor.

    Run the command git parent

    That's it!

提交回复
热议问题