开发者

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.

The branch master has a subdirectory lib that is based on the code that's on the branch upstream_lib;开发者_StackOverflow社区 changes in upstream_lib are merged (with the subtree strategy) to the master branch periodically. The lib directory in master has some modifications of its own that are not in 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.

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?


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. 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.
  2. Determine the fake common ancestor in the upstream_lib line, say upstream_lib~150.
  3. Make a throwaway copy of the upstream_lib branch: 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. (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:

    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.

  5. Merge it: git checkout master && git merge new_upstream_lib && git branch -d new_upstream_lib.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜