简体   繁体   English

如何更改Git子树合并的源代码

[英]How to change the source of a Git subtree merge

I have a project where I've merged in a library using Git subtree. 我有一个项目,我使用Git子树在库中合并。 I've pushed and pulled a few minor changes between the library and the project. 我推动了图书馆和项目之间的一些小改动。

Later on, a new repository has been created which is the definitive home for the library. 后来,创建了一个新的存储库,它是库的权威主页。 It contains essentially the same version of the library code as my project did, with perhaps one or two minor changes. 它包含与我的项目基本相同的库代码版本,可能有一两个小的更改。 For various reasons, it doesn't share any direct Git history with the previous home of the library (it is not a clone of the previous library). 由于各种原因,它不与图书馆的前一个主页共享任何直接的Git历史记录(它不是以前库的克隆)。

What I want to do now is change my project so that it pulls/pushes the library from the new location. 我现在要做的是更改我的项目,以便从新位置拉出/推送库。 The first time this happens I also need to resolve any merge conflicts, although in this case the changes are trivial and they could just be redone later. 第一次发生这种情况时,我还需要解决任何合并冲突,尽管在这种情况下,更改是微不足道的,它们可以稍后重做。

What is the best way to do this? 做这个的最好方式是什么?

I tried deleting taking a copy of the the library in my project, then removing it along with the old remotes and branches. 我尝试删除在我的项目中删除该库的副本,然后将其与旧的遥控器和分支一起删除。 I then tried doing subtree add etc from the new location. 然后我尝试从新位置进行子树添加等。 This seemed to work, but when I try to push back from my project to the library I'm getting a fatal bad object error. 这似乎有效,但当我尝试从我的项目推回到库时,我遇到致命的坏对象错误。

I assume that there's a flaw in the approach that I've tried - probably to do with the lack of a shared history - but I don't quite have a deep enough understanding of what's going on to know how to fix it, or what the "proper" approach to this problem should be. 我认为我尝试过的方法存在一个缺陷 - 可能与缺乏共享历史有关 - 但是我对已经知道如何解决它的问题还是没有足够的了解,或者是什么这个问题的“正确”方法应该是。

[update: edited the question to make it slightly clearer - it was a bit ambiguous] [更新:编辑问题使其更清晰 - 有点模棱两可]

Try cloning the new project. 尝试克隆新项目。 then adding yours as a remote and doing 然后添加你的遥控器和做

git remote update

This will pull in all the refs from your project. 这将从您的项目中提取所有参考。 now do 现在呢

git cherry-pick <sha1>

of the sha's that you want in the new project. 您想要在新项目中使用的sha。 That's the easiest way I think. 这是我想的最简单的方法。

Also you should know that git doesn't require a common history (though it makes for less nice merging the first time) for a merge. 另外你应该知道git不需要一个共同的历史记录(尽管它第一次合并的次数较少)。 so you could just do what I said and then instead of cherry-picking you could merge your history. 所以你可以做我说的话,然后你可以合并你的历史,而不是采摘樱桃。 it will probably have conflicts. 它可能会有冲突。 I suggest once you start the merge (if you do) use git mergetool and know how to use the 3-way diff. 我建议一旦你开始合并(如果你这样做)使用git mergetool并知道如何使用3-way diff。

Is there any reason you aren't referencing your library as a submodule from your project? 您是否有任何理由不将您的库作为项目的子模块引用?
(See true nature of submodules ) (参见子模块的真实性质

The advantage in your case would be how easy it is to change a submodule address . 在您的情况下,优势在于更改子模块地址是多么容易。

Perhaps, you should set up a separate working branch (or repositiory) with your version of the library (only the library tree with your changes) which you could then push back to the original library repo. 也许,您应该使用您的库版本(仅包含您的更改的库树)设置一个单独的工作分支(或存储库),然后您可以将其推回到原始库存储库。

So, in order to push to the original remote repo, first prepare locally what you expect to appear as a commit remotely; 因此,为了推送到原始远程仓库,首先在本地准备您希望远程提交的内容; there can be different workflows to achieve this: either, during the work on your project, you first commit your library changes to your special local working branch dedicated to the library (that branch should inherit the original history of the library), and then merge this branch with the library changes into your project working branch (where the library is a subtree), or merge your changes back from the project working branch into the dedicated library working branch. 可以有不同的工作流来实现这一点:或者,在项目工作期间,首先将库更改提交到专用于库的特殊本地工作分支(该分支应继承库的原始历史记录),然后合并这个带有库的分支将更改为您的项目工作分支(其中库是子树),或者将您的更改从项目工作分支合并到专用库工作分支中。 Then you can push your dedicated library working branch to the original remote repo when you wish. 然后,您可以根据需要将专用库工作分支推送到原始远程仓库。

So, essentially, first you create a local dedicated working branch so that it inherits the original history: 所以,基本上,首先你创建一个本地专用工作分支,以便它继承原始历史:

git branch MY_LIBFOO REMOTE_BRANCH_LIBFOO

(REMOTE_BRANCH_LIBFOO is your locally strored remote branch that interests you, which is updated by git fetch) (REMOTE_BRANCH_LIBFOO是您感兴趣的本地漫游远程分支,由git fetch更新)

then, following your workflow, make sure that your changes to the library are in the working branch MY_LIBFOO, and then you can 然后,按照您的工作流程,确保您对库的更改位于工作分支MY_LIBFOO中,然后您可以

git push original_libfoo MY_LIBFOO:TARGET_BRANCH_REMOTELY

This way, there is a clear idea of what is happening. 通过这种方式,可以清楚地了解正在发生的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM