4

假设我们有以下工作副本结构:

.
├── adm
└── etc

$ git remote -v
origin  git@github.com:xxx/my.git (fetch)
origin  git@github.com:xxx/my.git (push)

现在,假设我们通过以下方式添加了一个子项目git subtree

git remote add extlip git@github.com:yyy/ExtLib.git
git subtree add -P tech -m "added extlib as a sub-project" extlib/master

这样

.
├── adm
├── etc
└── tech

$ git remote -v
origin  git@github.com:xxx/my.git (fetch)
origin  git@github.com:xxx/my.git (push)
extlip  git@github.com:yyy/ExtLib.git (fetch)
extlip  git@github.com:yyy/ExtLib.git (push)

现在假设你有一段时间没有在这个项目上工作,你如何识别子项目的根?说,你如何识别你“子树化”的位置以及哪个是正确的遥控器?或者,你如何确定你“子树化”了?

4

1 回答 1

2

检测添加子树的提交的一种方法是查找两个父级不属于同一棵树的合并提交,或者换句话说,这两个提交在其历史记录中不共享任何先前的提交。

一个如何在 bash 中检测这种情况的示例脚本,从您的 git 存储库的根目录运行它:

#!/bin/bash

# To separate the rev-list by newlines
IFS=$'\n'

# This rev list will return the hash and the parents, separated by spaces,
# of any merge commit found in the history of HEAD
for hashes in $(git rev-list --merges --parents HEAD); do

    # To split the commits by space
    IFS=$' '
    hashList=($hashes)

    # Merge base will find the most recent commit shared by all the
    # given commits, output dumped just to not clutter
    git merge-base ${hashList[1]} ${hashList[2]} > /dev/null

    # We care only if such commit did not exist, which means each parent is
    # in its own separate tree
    if [[ $? != 0 ]]; then
        echo "Subtree merge: ${hashList[0]}"
    fi
done
unset IFS
于 2013-04-15T05:48:32.303 回答