英文:
Git rebase gives conflicts on remote branch, but works on local
问题
我有2个分支 - develop
和 master
,我在它们两个上面添加了新的提交,现在我想将 develop
重新基于 master
。当我执行以下命令时:
git checkout develop
git fetch
git rebase origin/master
它给我产生了大量冲突,我几乎无法解释,但如果我执行以下命令:
git checkout master
git pull origin HEAD
git checkout develop
git rebase master
它会正确地重新基于而不产生任何冲突。有人能解释一下我漏掉了什么吗?谢谢!
英文:
I have 2 branches - develop
and master
, I added new commits to both of them and now I want to rebase develop
on top of master
.
When I do
git checkout develop
git fetch
git rebase origin/master
it gives me tons of conflicts, which I can barely explain, but if I do
git checkout master
git pull origin HEAD
git checkout develop
git rebase master
it does a correct rebase without any conflicts.
Can someone explain me what am I missing here? Thanks!
答案1
得分: 2
-
git pull
的一个(重要)作用是将本地分支的更改与拉取的分支合并,结果是一个不同的提交, -
你在
origin/master
之上进行了变基,但拉取了HEAD
;如果你的远程默认分支不是master
(例如:对于github
仓库,默认分支现在命名为main
),那么结果可能会不同
在终端中运行 git log --oneline --graph HEAD master origin/master
命令(或者打开仓库历史的图形查看器,检查3个分支 HEAD master origin/master
),你将更好地了解包含了什么和没有包含什么。
英文:
Two possible points:
-
an (important) effect of
git pull
is to merge the changes in your local branch with the pulled branch, the result is a different commit, -
you rebased on top of
origin/master
but pulledHEAD
; if the default branch for your remote is not master (e.g: for agithub
repo, the default branch is now namedmain
) then the result can be different
Run git log --oneline --graph HEAD master origin/master
in a terminal (that or open a graphical viewer for the history of your repo, and check the 3 branches HEAD master origin/master
), you will have a better view of what is included and what isn't
答案2
得分: 0
建议在执行任何合并或变基操作之前,始终执行git pull
命令。这确保你的本地分支与远程分支保持同步,以避免由于其他人在此期间所做的更改而导致冲突。
当你在develop分支上执行git fetch
,然后跟随git rebase origin/master
时,它会从远程master分支获取最新的更改,但不会将这些更改应用到你的本地master分支。因此,当你尝试将develop分支变基到origin/master之上时,它必须解决由于你的本地master分支与远程master分支之间的差异而引起的冲突。
另一方面,当你在master分支上执行git pull
origin HEAD时,它会使用远程master分支的最新更改来更新你的本地master分支。因此,当你随后执行git checkout develop
,然后执行git rebase master
时,它能够将在master分支上进行的更改干净地应用到develop分支,而不会出现冲突。
总之,在开始任何变基或合并操作之前,始终从远程分支拉取最新的更改是一个良好的实践。
英文:
It is recommended to always perform a git pull
on the branch before starting any kind of merge or rebase operation. This ensures that your local branch is up-to-date with the remote branch, and there are no conflicts due to changes made by others in the meantime.
When you did git fetch
followed by git rebase origin/master
on the develop branch, it fetched the latest changes from the remote master branch but didn't apply those changes to your local master branch. So when you tried to rebase develop on top of origin/master, it had to resolve conflicts that arose due to the differences between your local master branch and the remote master branch.
On the other hand, when you did git pull
origin HEAD on the master branch, it updated your local master branch with the latest changes from the remote master branch. So when you then did git checkout develop
followed by git rebase master
, it was able to cleanly apply the changes made on the master branch to the develop branch without any conflicts.
In summary, it's always a good practice to pull the latest changes from the remote branch before starting any rebase or merge operations.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论