将子树移动到其他目录并拉出它

将子树移动到其他目录并拉出它

本文介绍了Git子树:将子树移动到其他目录并拉出它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Let's say I created a subtree like this:

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

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

Then I wanted to move/rename subdir, so I do this:git mv subdir dir2/subdir

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

Now when I try to pull that subtree to a new prefix:

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子树时将名称存储在第一次提交中:

The git subtree command knows about your subtree, because it stores the name in the first commit when you add a subtree:

Add 'subdir/' from commit 'c7fbc973614eced220dcef1663d43dad90676e00'

git-subtree-dir: subdir
git-subtree-mainline: c166dc69165f6678d3c409df344c4ed9577a2e11
git-subtree-split: c7fbc973614eced220dcef1663d43dad90676e00

带有--squash选项的

git subtree pull查找包含git-subtree-dir的提交,以从远程存储库中查找最新的提交,从而找到要应用和压缩所有提交的位置.

git subtree pull with the --squash option looks for a commit containing git-subtree-dir to find the most-recent commit from the remote repository and hence the point from which to apply and squash all commits.

在许多情况下,git subtree split --rejoin操作将成功:

In many cases a git subtree split --rejoin operation will be successful:

$ 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将成功.请注意,有时子树操作会失败,并使您在存储库的工作副本中留下子树的一个分支.确保删除所有残留的临时分支以干净的状态开始.

This commit will be found and the next git subtree pull --squash will succeed in most cases. Be aware that sometimes subtree operations fail and leave you with a branch of the subtree in your working copy of your repo. Make sure you delete any residual temporary branches to start with a clean slate.

有时上述操作不会成功,但我从未找到原因.在这些情况下,您可以改用添加了子树的提交,并通过修改提交消息来手动更改目录名称.但是,此操作将破坏您的所有其他用户的整个历史记录.

Sometimes the above operation does not succeed, but I never found the reason why. In those cases you can rebase to the commit that added the subtree and change the directory name manually by amending the commit message. This operation will corrupt your whole history for everybody else, though.

这篇关于Git子树:将子树移动到其他目录并拉出它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 03:33