【问题标题】:How to figure out a working copy contains a subtree?如何确定工作副本包含子树?
【发布时间】:2013-04-10 07:46:55
【问题描述】:

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

.
├── 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)

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

【问题讨论】:

    标签: git git-subtree


    【解决方案1】:

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

    如何在 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
    

    【讨论】:

    • 首先感谢您的回复!是的,使用此脚本,您可以回答“当前工作副本是否包含任何子树?”的问题。但与 git 的表达性消息相比,这并不是很方便。除此之外,我想知道子树的根在哪里,远程是什么,......类似的东西。但是,您的脚本是一个开始。再次感谢! /nm
    猜你喜欢
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 2011-02-22
    • 2022-11-15
    • 2012-02-03
    • 2019-03-30
    • 2011-03-07
    • 1970-01-01
    相关资源
    最近更新 更多