Find first common child of two commits

后端 未结 3 1797
旧时难觅i
旧时难觅i 2021-01-12 07:29
           :
           A
T         / \\
i        B   C
m        :   :
e        D   E
          \\ /
|          F
V          :

git merge-base

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-12 07:36

    Oops. Didn't read that carefully enough.

    The only information in a commit is the id of its parent (or parents). You cannot get to a child from a parent commit (this is the directed part of the repository being a DAG).

    Looking at this more - it looks like the --ancestry-path option for git log can do this. For instance given:

    * 85d26ab When compiling vim, also compile & install gvim
    *   3146e5d Merge remote-tracking branch 'origin/devel' into deve
    |\
    | * 28d08e5 rebasing-merge: specify all commits explicitly
    * | 006d11d Help 'file' find its magic file
    |/
    * e68531d (tag: Git-1.7.6-preview20110720) Update submodules
    

    we can get the all children of these two commits using

    git log --oneline --ancestry-path B..E
    

    if you then reverse this and pick off the first one -- that is F.

    git rev-list --reverse --ancestry-path 28d08e5..006d11d | head -1
    

    in my case that returns 3146e5d.

提交回复
热议问题