简体   繁体   English

如何在git合并中强制共同的祖先?

[英]How to force a common ancestor in a git merge?

Let's say I have a branch called master and a branch called upstream_lib . 假设我有一个名为master的分支和一个名为upstream_lib的分支。

The branch master has a subdirectory lib that is based on the code that's on the branch upstream_lib ; 分支master有一个子目录lib ,它基于分支upstream_lib上的代码; changes in upstream_lib are merged (with the subtree strategy) to the master branch periodically. upstream_lib中的更改会定期与master分支合并(使用子树策略)。 The lib directory in master has some modifications of its own that are not in upstream_lib . masterlib目录有自己的一些修改,不在upstream_lib

However, let's say the two branches either have no common history (because the repository was just migrated to git, for instance) or the merge base is incorrect because the merges in upstream_lib have been squashed, there has been some rebasing or whatever. 但是,假设两个分支没有共同的历史记录(例如,因为存储库刚刚迁移到git),或者合并基础不正确,因为upstream_lib的合并已经被压缩,有一些变基或其他什么。

The question is: given a new set of changes on upstream_lib , how to force the merge to consider as the common ancestor a specific revision of upstream_lib ? 现在的问题是:给出一组新的变化upstream_lib ,如何强制合并考虑的共同祖先的一个特定修订upstream_lib

I've never used the subtree strategy, so maybe this is a suboptimal solution (and maybe it won't work ^^), but you could apply all the new commits in upstream_lib to a temporary branch off of the master line and then merge that. 我从来没有使用的subtree的策略,所以也许这是一个次优的解决方案(也许它不会工作^^),但你可以应用所有新提交upstream_lib到临时党支部关闭的master行,然后合并那。 What I have in mind doesn't fundamentally fix your situation, so you'll have to do this kind of "manual merge" every time you want to pull in new changes, but here's how it works: 我想到的并不是从根本上解决你的情况,所以每次你想要引入新的变化时你都必须做这种“手动合并”,但这是它的工作原理:

  1. Determine the fake common ancestor in the master ancestry line, say master~100 . 确定master祖先行中的假共同祖先,比如master~100
  2. Determine the fake common ancestor in the upstream_lib line, say upstream_lib~150 . 确定upstream_lib行中的伪共同祖先,比如upstream_lib~150
  3. Make a throwaway copy of the upstream_lib branch: git branch --no-track new_upstream_lib upstream_lib 制作一个关于upstream_lib分支的一次性副本: git branch --no-track new_upstream_lib upstream_lib
  4. Rebase new_upstream_lib onto master~100 using the recursive strategy with the subtree option. 使用带有子树选项的递归策略将new_upstream_lib引导到master~100 (I don't think you can just use the subtree strategy because, as you say, the lib directory in master has changes of its own.) Here's a completely untested command for this: (我认为你不能只使用子树策略,因为正如你所说, masterlib目录有自己的变化。)这是一个完全未经测试的命令:

     git rebase -s recursive -X subtree=lib --onto master~100 upstream_lib~150 new_upstream_lib 

    Note that new_upstream_lib now has the whole master tree in it, even though you only care about the lib directory. 请注意, new_upstream_lib现在包含整个master树,即使您只关心lib目录。

  5. Merge it: git checkout master && git merge new_upstream_lib && git branch -d new_upstream_lib . 合并它: git checkout master && git merge new_upstream_lib && git branch -d new_upstream_lib

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

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