简体   繁体   English

如何在Git中用叉子建立共同的祖先?

[英]How to establish a common ancestry with a fork in Git?

I have a git repository containing a fork of project foo: 我有一个git存储库,其中包含foo项目的分支:

origin/master
+src/
|  +foo/
|     +A/
|     +B/
|     +C/
+stuff/

I also have a mirror of project foo: 我也有一个foo项目的镜像:

upstream/master
+foo/
   +A/
   +B/
   +C/

I do not yet have in git a common ancestry. 我还没有git的共同血统。 I would like to both establish a common ancestry to make future merges easier and merge in any changes from the upstream foo that have been made since I forked. 我想建立一个共同的祖先,以使将来的合并变得更容易,并合并自从我分叉以来对上游foo所做的任何更改。

I attempted to rebase my changes to foo on top of the upstream: 我试图将对foo更改重新建立在上游之上:

C:\src>git rebase -s recursive -X subtree=foo/ --root upstream/master

But, that did not work the way I desired. 但是,这并没有达到我想要的方式。 I ended up with origin/master looking exactly like upstream/master with none of my changes included and completely missing stuff . 最后,我的origin/master看起来完全像upstream/master ,没有任何更改,也完全没有stuff

origin/master
+foo/
   +A/
   +B/
   +C/

What do I need to do to merge upstream foo with my src/foo ? 我需要怎么做才能将上游foo与我的src/foo合并?

Am I limited to doing something like this? 我仅限于做这样的事情吗?

C:\src>git merge upstream/master -s recursive -X subtree=foo/

My suggestion would be to use a git filter-branch to rewrite upstream as if foo/ had been src/foo/ from the start. 我的建议是使用git filter-branch改写upstream仿佛foo/曾经src/foo/从一开始。 Based on https://stackoverflow.com/a/3212697/54249 : 基于https://stackoverflow.com/a/3212697/54249

git filter-branch --commit-filter '
    TREE="$1";
    shift;
    SUBTREE=`echo -e 040000 tree $TREE"\tsrc" | git mktree`
    git commit-tree $SUBTREE "$@"' -- --all

That should give us: 那应该给我们:

upstream/master
+src/
   +foo/
      +A/
      +B/
      +C/

At this point you should be able to simply merge upstream/master into origin/master , giving a git log --oneline --graph --decorate that looks something like this: 在这一点上,您应该能够简单地将upstream/master合并到origin/master ,给出一个git log --oneline --graph --decorate看起来像这样:

*    (HEAD, master) Merge branch 'master' of upstream
|\
| *  (upstream/master) fourth upstream commit
| *  third upstream commit
* |  (origin/master) fourth origin commit
| *  second upstream commit
* |  third origin commit
| *  first upstream commit
*    second origin commit
*    first origin commit

Here's a proof of concept for a pair of local repositories - note the git pull reports "warning: no common commits": 这是一对本地存储库的概念证明-请注意git pull报告“警告:没有常见的提交”: 概念验证

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

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