Finding a branch point with Git?

前端 未结 22 1348
小鲜肉
小鲜肉 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:45

    surely I'm missing something, but IMO, all the problems above are caused because we are always trying to find the branch point going back in the history, and that causes all sort of problems because of the merging combinations available.

    Instead, I've followed a different approach, based in the fact that both branches share a lot of history, exactly all the history before branching is 100% the same, so instead of going back, my proposal is about going forward (from 1st commit), looking for the 1st difference in both branches. The branch point will be, simply, the parent of the first difference found.

    In practice:

    #!/bin/bash
    diff <( git rev-list "${1:-master}" --reverse --topo-order ) \
         <( git rev-list "${2:-HEAD}" --reverse --topo-order) \
    --unified=1 | sed -ne 's/^ //p' | head -1
    

    And it's solving all my usual cases. Sure there are border ones not covered but... ciao :-)

提交回复
热议问题