Git rebase在远程分支上冲突,但在本地分支上可以正常工作。

huangapple go评论54阅读模式
英文:

Git rebase gives conflicts on remote branch, but works on local

问题

我有2个分支 - developmaster,我在它们两个上面添加了新的提交,现在我想将 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 pulled HEAD ; if the default branch for your remote is not master (e.g: for a github repo, the default branch is now named main) 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.

huangapple
  • 本文由 发表于 2023年5月22日 14:03:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303391.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定