【问题标题】:Adding git subtree from a branch从分支添加 git 子树
【发布时间】:2013-05-25 15:05:53
【问题描述】:

我正在尝试使用 git subtree add 向我的项目添加一个 repo(称为牛)。特别是,我想添加分支stable(不是master 分支)。我试过了:

git subtree add -P cow https://github.com/geoffryan/cow.git stable

但这返回了错误

'stable' does not refer to a commit.

我也试过了:

git subtree add -P cow https://github.com/geoffryan/cow.git cow/stable
'cow/stable' does not refer to a commit.

还有:

git subtree add -P cow https://github.com/geoffryan/cow.git ca26d248a12c21264e32a2c212381cafb578c9fb
'ca26d248a12c21264e32a2c212381cafb578c9fb' does not refer to a commit.

哈希是stable 分支中最新提交的哈希。我在网上看到的使用示例都使用master进行提交,是否可以在非master分支上使用subtree add

【问题讨论】:

    标签: git git-subtree


    【解决方案1】:

    这似乎有效

    $ git remote add cow https://github.com/geoffryan/cow.git
    $ git fetch cow
    $ git subtree add -P cow cow/stable
    Added dir 'cow'
    

    我不明白如何直接将命令与存储库部分一起使用。

    【讨论】:

    • 但这会污染父 repo,因为来自牛 repo 的一堆不需要的提交 ... :-/
    • 哪些是不需要的提交?
    • 这可能有点晚,但您可以使用 --squash 防止 git subtree 重播整个日志。
    • 你可能想要添加 --squash 选项,这样 git subtree 将只从子项目中导入一个提交,而不是它的整个历史。
    • 澄清一点,因为@rpattabi 编辑了答案:--squash 如果您使用git subtree,这不是正确的选项 IHMO bc,您通常希望保留原始历史记录,否则您可以简单地复制和像往常一样提交文件。
    【解决方案2】:

    我遇到了类似的问题。但是,gipi 的解决方案对我不起作用。奇怪的是,当我添加 master 分支时一切都很好,但是当我想添加其他分支时,它返回

    fatal: Couldn't find remote ref xxx/yyy
    Unexpected end of command stream
    

    所以我尝试了另一种方法:

    mkdir tmp
    cd tmp
    git init
    git clone url_for_xxx.git yyy
    

    还有来自那个临时代表的主分支的subtree add

    git subtree add -P yyy /path/to/tmp/ master 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-11
      • 1970-01-01
      • 2010-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-17
      相关资源
      最近更新 更多