不知何故,我的 master
和我的 origin/master
分支发生了分歧。
我实际上不希望它们发生分歧。
如何查看这些差异并 merge 它们?
最佳答案
您可以 review the differences有一个:
git log HEAD..origin/main
# old repositories
git log HEAD..origin/master
之前pulling it (获取 + merge )(另见 "How do you get git to always pull from a specific branch?" )
注:自 Git 2.28 (Q3 2020) ,默认分支是可配置的,现在(2021+)设置为main
,不再是master
。
其余答案反射(reflect)了最近的约定。
当您收到如下消息时:
"Your branch and 'origin/main' have diverged, # and have 1 and 1 different commit(s) each, respectively."
检查你是否need to update origin
.
如果 origin
是最新的,那么当您在本地进行自己的提交时,一些提交已从另一个 repo 推送到 origin
。
... o ---- o ---- A ---- B origin/main (upstream work)
\
C main(your work)
你基于提交 A
提交 C
因为那是你当时从 upstream
获取的最新工作。
但是,在您尝试返回到 origin
之前,其他人推送了提交 B
。
发展历程分道扬镳。
然后您可以 merge 或 rebase 。参见 Pro Git: Git Branching - Rebasing了解详情。
merge
使用git merge
命令:
$ git merge origin/main
# old repositories
$ git merge origin/master
这告诉 Git 将来自 origin/main
的更改整合到您的工作中并创建 merge 提交。
历史图现在看起来像这样:
... o ---- o ---- A ---- B origin/main (upstream work)
\ \
C ---- M main (your work)
新的 merge ,提交 M
,有两个父级,每个父级代表一条开发路径,导致内容存储在该提交中。
请注意,M
背后的历史现在是非线性的。
rebase
使用git rebase
命令:
$ git rebase origin/main
# old repositories
$ git rebase origin/master
这告诉 Git 重播提交 C
(您的工作),就好像您是基于提交 B
而不是 A
一样。
CVS 和 Subversion 用户在提交前更新时,通常会在 upstream
工作之上重新设置他们的本地更改。
Git 只是在提交和 rebase 步骤之间添加了明确的分隔。
历史图现在看起来像这样:
... o ---- o ---- A ---- B origin/main (upstream work)
\
C' main (your work)
Commit C'
是由git rebase
命令创建的新提交。
它在两个方面不同于 C
:
- 它有不同的历史:
B
而不是A
。 - 其内容说明了
B
和C
中的变化;它与 merge 示例中的M
相同。
请注意,C'
背后的历史仍然是线性的。
我们选择(目前)在 cmake.org/cmake.git
中只允许线性历史。
这种方法保留了以前使用的基于 CVS 的工作流程,并可能简化过渡。
尝试将 C'
推送到我们的存储库中将会成功(假设您有权限并且在您 rebase 时没有人推送)。
git pull
命令提供了一种从 origin
fetch
和 rebase
本地工作的简写方式:
$ git pull --rebase
这将上面的 fetch
和 rebase
这两个步骤 merge 到一个命令中。
关于git - master分支和 'origin/master' fork 了,怎么 'undiverge'分支'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2452226/