[英]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?
我究竟做错了什么?
call the parent project 'upstream'
将父项目称为“上游”
git pull --rebase upstream master
git pull-重新设置上游主机
git push origin master
git push origin master
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
也就是说,上游具有新的提交
d
和e
,而您已经进行了新的提交f
, g
和h
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. 当
f
和f'
不是相同的提交时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'
? h
或h'
? 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. 您可以合并这些,但是那是错误的,因为您在
f
和f'
, g
和g'
等中具有相同的更改。
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 这会将
f
, g
和h
从fork中丢弃,并使它看起来像本地的 (您仍然有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.