Big*_*r00 6 git gitlab git-fork
以下一系列 git 命令导致存储库出现分歧。我究竟做错了什么?
将父项目称为“上游”
git pull --rebase 上游 master
git推送原点大师
git pull最终会扰乱历史的事情。这样做的正确方法是什么?
Spo*_*ser 10
让我们解决这个问题。
有 3 个存储库:您的本地克隆、上游、您的分支(源自本地克隆)
第 2 步之后,它们看起来像这样:
上游
o---o---o
a b c
Run Code Online (Sandbox Code Playgroud)
叉
o---o---o
a b c
Run Code Online (Sandbox Code Playgroud)
当地的
o---o---o
a b c
Run Code Online (Sandbox Code Playgroud)
第 5 步之后,存储库看起来像这样:
上游
o---o---o---o---o
a b c d e
Run Code Online (Sandbox Code Playgroud)
叉
o---o---o---o---o---o
a b c f g h
Run Code Online (Sandbox Code Playgroud)
当地的
o---o---o---o---o---o
a b c f g h
Run Code Online (Sandbox Code Playgroud)
也就是说,上游有新的提交d和e,并且您已经进行了新的提交f,g并且h
你现在做git pull --rebase upstream master
存储库现在看起来像这样:
上游
o---o---o---o---o
a b c d e
Run Code Online (Sandbox Code Playgroud)
叉
o---o---o---o---o---o
a b c f g h
Run Code Online (Sandbox Code Playgroud)
当地的
o---o---o---o---o---o---o---o
a b c d e f' g' h'
Run Code Online (Sandbox Code Playgroud)
其中f和f'不是相同的提交 -f'应该相当于f,但它有不同的父级。
你可以在这里看到,本地现在有不同的历史要分叉;推动它不仅仅是添加新提交的情况。两者都认为 后有不同的提交c,并且两个分支不收敛;如果您添加了所有提交,您最终会得到以下图表:
,-----------o---o---o
| f g h
o---o---o---o---o---o---o---o
a b c d e f' g' h'
Run Code Online (Sandbox Code Playgroud)
当前的 HEAD 是什么?h或者h'?两者都保留了对方的历史。
您可以合并这些,但这将是错误的,因为您在 和 等中有等效f的更改。f'gg'
你可以做
git push -f
Run Code Online (Sandbox Code Playgroud)
这会从fork中丢弃f,g并使其看起来像本地的(你仍然有, ),如果没有其他人从fork克隆,这可能没问题hf'g'h'
在第 6 步,您可以不执行变基操作,而是执行以下操作
git pull upstream master
Run Code Online (Sandbox Code Playgroud)
这会导致合并,因此存储库将如下所示:
上游
o---o---o---o---o
a b c d e
Run Code Online (Sandbox Code Playgroud)
叉
o---o---o---o---o---o
a b c f g h
Run Code Online (Sandbox Code Playgroud)
当地的
,---o---o---o---,
| f g h |
o---o---o---o---o-------o
a b c d e m
Run Code Online (Sandbox Code Playgroud)
m合并提交在哪里。然后,这可以被简单地推送到fork,因为它只是添加额外的提交。
不过,如果您计划向上游发出拉取请求,也许最好不要合并他们的更改,并让他们拉取并处理合并。
| 归档时间: |
|
| 查看次数: |
1822 次 |
| 最近记录: |