【问题标题】:Git subtree: move subtree to a different directory and pull itGit子树:将子树移动到不同的目录并拉取它
【发布时间】:2017-09-05 13:01:36
【问题描述】:

假设我创建了一个这样的子树:

git subtree --add --prefix=subdir <path_to_remote> <remote_branch> --squash

然后我想移动/重命名子目录,所以我这样做: git mv subdir dir2/subdir

现在,当我尝试将该子树拉到新前缀时:

git subtree --pull --prefix=dir2/subdir <path_to_remote> <remote_branch> --squash

git 说:

Can't squash-merge: 'dir2/subdir' was never added.

我怎样才能正确地做到这一点?

【问题讨论】:

    标签: git git-subtree


    【解决方案1】:

    git subtree 命令知道您的子树,因为当您add 子树时,它会将名称存储在第一次提交中:

    Add 'subdir/' from commit 'c7fbc973614eced220dcef1663d43dad90676e00'
    
    git-subtree-dir: subdir
    git-subtree-mainline: c166dc69165f6678d3c409df344c4ed9577a2e11
    git-subtree-split: c7fbc973614eced220dcef1663d43dad90676e00
    

    git subtree pull--squash 选项查找包含 git-subtree-dir 的提交,以从远程存储库中找到最近的提交,从而找到应用和压缩所有提交的点。

    在很多情况下git subtree split --rejoin 操作会成功:

    $ git subtree split --rejoin --prefix=dir2/subdir HEAD
    Merge made by the 'ours' strategy.
    25070c483647f8136655d0e0c6c3d62f469177aa
    

    生成的提交如下所示:

    Split 'dir2/subdir/' into commit '25070c483647f8136655d0e0c6c3d62f469177aa'
    
    git-subtree-dir: dir2/subdir
    git-subtree-mainline: 59cc3c770e78dbc30bdfe36a6b4e14ce83b38f6c
    git-subtree-split: 25070c483647f8136655d0e0c6c3d62f469177aa
    

    这个提交会被找到,并且下一个git subtree pull --squash 在大多数情况下都会成功。请注意,有时子树操作会失败,并在您的存储库的工作副本中留下子树的一个分支。确保删除所有剩余的临时分支以重新开始。

    有时上面的操作不成功,但是我一直没找到原因。在这些情况下,您可以重新定位到添加子树的提交,并通过修改提交消息手动更改目录名称。不过,此操作会破坏您的整个历史记录。

    【讨论】:

    • 谢谢,这对我有用!既然你说 git subtree 只是在提交消息中查找文本,我尝试添加必要的文本以重命名提交,但是 - 正如你所说 - 在我的工作副本中以子树的一个分支结束。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多