git 将子目录推送到另一个主存储库中
我有一个很大的主项目,有几个目录作为子树。
我想将一个特定子树中的更改推送到其原始位置,这是一个单独的存储库。
问题似乎是,我想要推送的当前子树最初并不是来自我想要推送到的存储库。它来自另一个存储库,通过我通过谷歌搜索找到的子树指南。它只是看起来很相似。
大项目布局,其中 important_subtree
是我担心的事情。
~/devel/bigproject
.git/
some_subtree/
other_subtree/
important_subtree/
abc.txt
efg.txt <--- new version
hij.txt
并且 important_subtree
与该存储库“密切相关”:
~/devel/important
.git/
abc.txt
efg.txt <--- old version
hij.txt
现在 ~/devel/bigproject/important_subtree/efg.txt
已更改,我想推送 important_subtree 到存储库~/devel/important
。所以之后~/devel/important/efg.txt
也有变化。
我唯一能做的就是将bigproject中的所有推入重要,这显然不是我想要的。仅应推送子树中的更改。
I have a big main project with several directories as subtrees.
I want to push the changes in one specific subtree to its origin, which is a separate repository.
The trouble seems to be, that the current subtree I want to push does not originally came from the repository I want to push into. It came from a different repo, via the subtree guides I found by googling. It just looks very similar.
Big project layout, where important_subtree
is the thing I am worried about.
~/devel/bigproject
.git/
some_subtree/
other_subtree/
important_subtree/
abc.txt
efg.txt <--- new version
hij.txt
And the important_subtree
is "strongly related" to that repo:
~/devel/important
.git/
abc.txt
efg.txt <--- old version
hij.txt
Now ~/devel/bigproject/important_subtree/efg.txt
has changed and I want to push the important_subtree onto the repo ~/devel/important
. So afterwards ~/devel/important/efg.txt
also has the changes.
The only thing I managed to do is to push push everything in bigproject into important, which is obviously not what I want. Only the changes in the subtree should be pushed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不再那么复杂,您只需使用 git filter-branch 命令即可在您的存储库的克隆上,剔除您不需要的子目录,然后推送到新的远程目录。
This is no longer so complex, you can just use the git filter-branch command on a clone of your repo to cull the subdirectories you don't want and then push to the new remote.
我建议在 git 中添加 git-subtree 。它添加了一个 git subtree split 命令来执行您想要的操作。
自您将
important
合并为子树以来,此选择会优先选择bigproject
中的更改,仅采用那些修改了important_subtree/
的更改。然后,它将它们作为新提交应用到从~/devel/important
导入的提交之上,并创建一个分支backport
,您可以以正常方式推回该分支。此外,--rejoin
也可以实现这一点,因此如果您想在更多更改上重复该过程,则将来无需使用提交 ID。作者的博客文章中有更多解释。
I would recommend the git-subtree addition to git. It adds a
git subtree split
command that does what you want.This cherry-picks the changes in
bigproject
since you mergedimportant
as a subtree, taking only those that modifiedimportant_subtree/
. It then applies them as new commits on top of the commit you imported from~/devel/important
and creates a branchbackport
that you can push back in the normal way. Also--rejoin
makes it so you don't need to use commit IDs in the future if you want to repeat the process on more changes.There is more explanation on the author's blog post.
使用不同的分支进行推送可能是一件非常棘手的事情。
也许最简单的方法是:
首先推送(例如 git format-patch)(如果您的原始提交混合了应该推送的文件和不应该推送的文件,您可能需要首先在侧分支中进行“较小的”提交)
Pushing with different branches can be a very tricky thing.
Maybe the easiest way would be:
push in the first place (e.g. git format-patch) (you might need to make "smaller" commits in a side branch first, if your original commits mix files that should be pushed and such files that shouldn't)