Git 子树 : move subtree to a different directory and pull it

标签 git git-subtree

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

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

然后我想移动/重命名子目录,所以我这样做:git mv subdir dir2/subdir
现在,当我尝试将该子树 pull 到新前缀时:
git subtree --pull --prefix=dir2/subdir <path_to_remote> <remote_branch> --squash

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

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

最佳答案

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 子树 : move subtree to a different directory and pull it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46055656/

相关文章:

Git 子树 merge 策略 - 设置本身如何跨克隆进行?

git - 如何删除以前添加的 git 子树及其历史记录

git - 从 git 子树获取上游分支

git - 包管理器与 Git 子模块/子树

git - Sourcetree - 撤消删除的文件

git - 如果 Git Stash 保存了 Diff,那么为什么不能将其应用到不同的分支呢?

git - 如何在 OpenShift 部署中指定除 master 之外的特定分支?

git - 在个人 git repo 上使用 "go get"

git - 使用git查找文件特定行首次引入token

git - 克隆git repo的子树/子目录