Git - how to find first commit of specific branch

后端 未结 7 466
长发绾君心
长发绾君心 2020-11-30 18:35

In following example tree:

A-B-C-D-E (master branch)
    \\
     F-G-H (xxx branch)

I\'m looking for F - the first commit in xxx branch. I

7条回答
  •  孤街浪徒
    2020-11-30 19:09

    If your branch (old one) once again merged back to master doesn't give expected result.I have using python script to find initial branch commit Id.

    git rev-list --first-parent changeset

    --first-parent follow only the first parent commit upon seeing a merge commit.

    Iterate changeset's from above command until parent branch found.

    def status_check(exec_command, exec_dir=None, background=False):
        if exec_dir:
            os.chdir(exec_dir)
        res = subprocess.Popen(exec_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        if not background:
            result = res.communicate()
        return result
    
    
    
    def findNewBranchCommits(changeset=None):
        cmd = "git rev-list --first-parent "+ changeset
        rev_list = status_check(cmd,self.module_dir)
        rev_list = str(rev_list[0]).split('\n')
        rev_list = list(filter(None, rev_list))
        for x in rev_list:                      # Iterate until branch base point
            rev_cmd = "git branch --contains " + x
            rev_cmd = status_check(rev_cmd,self.module_dir)
            rev_cmd = str(rev_cmd[0]).split('\n')
            if(len(rev_cmd) > 2): 
                print "First Commit in xxx branch",x
                break
    
    findNewBranchCommits(changeset)
    

提交回复
热议问题