git 分支在 rebase 后出现了分歧,那么为什么 rebase 呢?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34678950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 11:41:05  来源:igfitidea点击:

Git branch has diverged after rebase, so why rebase?

gitbranchgit-mergegit-rebase

提问by Amber

Recently I came across the notification that my branch has diverged. That was when I made a feature branch, pushed it to remote, and did a rebase with master a few days later when I started working on it again.

最近,我遇到了我的分支已经发散的通知。那时我创建了一个功能分支,将其推送到远程,并在几天后重新开始工作时与 master 进行了 rebase。

git checkout -b feature-branch
git push origin feature-branch:feature-branch

...and when in master...

...当在主人...

git pull origin master
git checkout feature-branch
git rebase master

But when I want to push my branch again, it says:

但是当我想再次推送我的分支时,它说:

On branch feature-branch
Your branch and 'origin/feature-branch' have diverged,
and have 67 and 1 different commit each, respectively.

I found this answer in "Git branch diverged after rebase":

我在“ Git branch diverged after rebase”中找到了这个答案:

Since you'd already pushed the branch, you should have merged in the source branch, rather than rebasing against it.

由于您已经推送了分支,因此您应该在源分支中进行合并,而不是针对它进行变基。

Question

After reading thisI still don't fully understand what I should've done differently in my flow, and why I still want to be using git rebase. Hope someone can explain this, thanks!

读完这篇文章后,我仍然不完全理解我应该在我的流程中做些什么不同的事情,以及为什么我仍然想使用 git rebase。希望有人能解释一下,谢谢!

回答by VonC

The idea is to rebase only if you haven't pushed yet, to replay your localcommits.

这个想法是只有在您还没有推送时才重新设置基准,以重播您的本地提交。

As soon as you have pushed (and are working in a team), you should not rebase the branch on top of master, as it rewrites its SHA1, forcing you to force push the new state of the branch.

一旦您推送(并在团队中工作),您就不应该在 之上master重新设置分支,因为它会重写其 SHA1,迫使您强制推送分支的新状态。

http://rypress.com/tutorials/git/media/5-1.png

http://rypress.com/tutorials/git/media/5-1.png

Making a git merge masterinto your branch is preferable here: you resolve the conflicts locally, then you can more commits, and a regular push.

git merge master在这里最好将 a放入您的分支:您在本地解决冲突,然后您可以进行更多提交和定期推送。

http://i.stack.imgur.com/wBMwq.png

http://i.stack.imgur.com/wBMwq.png

See more at "What is the difference between merging masterinto branch and merging branch into master?"

请参阅“合并master到分支和合并分支到有master什么区别?