Finding a branch point with Git?

前端 未结 22 1556
小鲜肉
小鲜肉 2020-11-22 10:11

I have a repository with branches master and A and lots of merge activity between the two. How can I find the commit in my repository when branch A was created based on mast

22条回答
  •  日久生厌
    2020-11-22 10:40

    I believe I've found a way that deals with all the corner-cases mentioned here:

    branch=branch_A
    merge=$(git rev-list --min-parents=2 --grep="Merge.*$branch" --all | tail -1)
    git merge-base $merge^1 $merge^2
    

    Charles Bailey is quite right that solutions based on the order of ancestors have only limited value; at the end of the day you need some sort of record of "this commit came from branch X", but such record already exists; by default 'git merge' would use a commit message such as "Merge branch 'branch_A' into master", this tells you that all the commits from the second parent (commit^2) came from 'branch_A' and was merged to the first parent (commit^1), which is 'master'.

    Armed with this information you can find the first merge of 'branch_A' (which is when 'branch_A' really came into existence), and find the merge-base, which would be the branch point :)

    I've tried with the repositories of Mark Booth and Charles Bailey and the solution works; how couldn't it? The only way this wouldn't work is if you have manually changed the default commit message for merges so that the branch information is truly lost.

    For usefulness:

    [alias]
        branch-point = !sh -c 'merge=$(git rev-list --min-parents=2 --grep="Merge.*$1" --all | tail -1) && git merge-base $merge^1 $merge^2'
    

    Then you can do 'git branch-point branch_A'.

    Enjoy ;)

提交回复
热议问题