简体   繁体   English

如何防止分叉的git repo发散

[英]How to prevent forked git repo from diverging

The following series of git commands is leading to repos to diverge. 以下一系列git命令导致存储库有所不同。 What am I doing wrong? 我究竟做错了什么?

  1. Fork project from GitLab GitLab的Fork项目

    call the parent project 'upstream' 将父项目称为“上游”

  2. Clone repo from forked project 分叉项目的克隆仓库
  3. Make edits locally in branch master 在分支母版中本地进行编辑
  4. Commit local edits, push to forked repo 提交本地编辑,推送到分叉的仓库
  5. upstream repo has commits from other developers 上游回购已从其他开发人员提交
  6. grab latest commits from upstream repo 从上游仓库获取最新提交

    git pull --rebase upstream master git pull-重新设置上游主机

  7. incorporate the latest commits from upstream that are now local into forked repo 将上游的最新提交(现在是本地的)合并到分叉的仓库中

    git push origin master git push origin master

  8. git says my branch has diverged by x commits and y commits, respectively. git说我的分支分别被x提交和y提交分开了。 Asks me to do a git pull which ultimately clutters the history. 请我进行一次git pull ,最终使历史混乱。

What is the right way to do this? 什么是正确的方法?

Lets work this through. 让我们来解决这个问题。

There are 3 repositories: Your local clone, upstream, your fork (origin to your local clone) 有3个存储库:您的本地克隆,上游,您的fork(起源于您的本地克隆)

After step 2, they look something like this: 步骤2之后,它们看起来像这样:

upstream 上游的

o---o---o
a   b   c

fork 叉子

o---o---o
a   b   c

local 本地

o---o---o
a   b   c

After step 5, the repos look something like this: 在第5步之后,存储库如下所示:

upstream 上游的

o---o---o---o---o
a   b   c   d   e

fork 叉子

o---o---o---o---o---o
a   b   c   f   g   h

local 本地

o---o---o---o---o---o
a   b   c   f   g   h

That is, upstream has new commits d and e , and you've made new commits f , g and h 也就是说,上游具有新的提交de ,而您已经进行了新的提交fgh

Now, you do git pull --rebase upstream master 现在,您执行git pull --rebase upstream master

The repositories now look like this: 现在,存储库如下所示:

upstream 上游的

o---o---o---o---o
a   b   c   d   e

fork 叉子

o---o---o---o---o---o
a   b   c   f   g   h

local 本地

o---o---o---o---o---o---o---o
a   b   c   d   e   f'  g'  h'

Where f and f' are not the same commit - f' is supposed to be equivalent to f , but it has a different parent. ff'不是相同的提交时f'应该等效于f ,但是它具有不同的父对象。

You can see here, that local has a different history now to fork ; 你可以在这里看到, 当地有不同的历史,现在到餐桌 ; pushing to it isn't simply a case of adding new commits. 推动它不仅仅是添加新提交的情况。 Both think a different commit comes after c , and the two branches do not converge; 两者都认为在c之后有一个不同的提交,并且两个分支不收敛。 if you added all the commits, you'd end up with this graph: 如果添加了所有提交,最终将得到以下图形:

        ,-----------o---o---o
        |           f   g   h
o---o---o---o---o---o---o---o
a   b   c   d   e   f'  g'  h'

And what would be the current HEAD? 而当前的HEAD是什么? h or h' ? hh' Neither preserve the history of the other. 两者都不保留对方的历史。

You could merge these, but that would be wrong because you have equivalent changes in f and f' , g and g' etc. 您可以合并这些,但是那是错误的,因为您在ff'gg'等中具有相同的更改。

You could do 你可以做

git push -f

This would throw away f , g and h from fork and make it look like local (you'd have f' , g' and h' still), which is probably fine if no one else has cloned from fork 这会将fghfork中丢弃,并使它看起来像本地的 (您仍然有f'g'h' ),如果没有其他人从fork中克隆,这可能很好

At step 6, instead of doing the rebase, you could have done 在第6步中,您可以做的不是重新设置基准

git pull upstream master

Which would have resulted in a merge, so the repos would look like this: 这将导致合并,因此存储库如下所示:

upstream 上游的

o---o---o---o---o
a   b   c   d   e

fork 叉子

o---o---o---o---o---o
a   b   c   f   g   h

local 本地

        ,---o---o---o---,
        |   f   g   h   |
o---o---o---o---o-------o
a   b   c   d   e       m

Where m is a merge commit. 其中m是合并提交。 This could then by trivially pushed to fork because it's just adding extra commits. 然后,这可能会被轻而易举地分叉,因为它只是添加了额外的提交。

If you're planning to issue a pull request to upstream, though, maybe better not to merge their changes in, and let them pull and handle the merge. 但是,如果您打算向上游发出拉取请求,则最好不要合并其更改,并让他们拉并处理合并。

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

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