在公共功能分支中使用 git rebase

发布于 2024-08-18 01:33:43 字数 1681 浏览 2 评论 0原文

您可以在网络上看到人们建议不要在公共分支中使用 git rebase,但如果您总是对功能分支进行 rebase,我看不出问题是什么。

我的团队总是使用分支来实现功能(哇),我们习惯于在本地拥有它,所以变基不是问题,但有时我们想向其他开发人员展示部分完成的功能的代码,所以我们只是将其公开,但这样我们就失去了 git rebase 的所有优点,或者至少你可以在网上读到这些优点。

我不明白问题是什么,如果在同一个公共分支工作的开发人员从未将其合并到任何分支(当该分支上仍有开发时),并且当他们拉动它时,他们会通过变基操作来完成。对分支所做的任何更改都将始终在远程分支之上进行变基,因此永远不会丢失,并且您不会遇到重复相同提交的问题。

附录1:

到目前为止,所有答案都没有表明将会发生的问题以及它将如何发生,所以我会尽力说得更清楚。

我将给出一个使用 rebase 的工作流程示例(在前面的段落中描述得很糟糕,抱歉),我没有看到任何问题。

初始状态:

master         ==========A
origin/feature           +=====AB
feature user A           +=====AB
feature user B           +=====AB

master 获得一些提交,用户 A 进行一些提交:

master         ==========A=====C
origin/feature           +=====AB
feature user A           +=====AB====D
feature user B           +=====AB

用户 A 执行 git pull --rebase (他总是这样做)来更新他的分支,没有新的内容出现,然后他 rebase掌握并推送:(

master         ==========A=====C
origin/feature                 +=====ACB'=====ACB'D
feature user A                 +=====ACB'=====ACB'D
feature user B           +=====AB

注意 B' 是仍然代表更改 B 的新提交)

然后用户 B 进行一些提交:

master         ==========A=====C
origin/feature                 +=====ACB'=====ACB'D
feature user A                 +=====ACB'=====ACB'D
feature user B           +=====AB======E

用户 B 最后像往常一样执行 git pull --rebase不需要在 master 上重新建立基础,所以他只需推送:

master         ==========A=====C
origin/feature                 +=====ACB'=====ACB'D======E'
feature user A                 +=====ACB'=====ACB'D
feature user B                 +=====ACB'=====ACB'D======E'

You can see all over the web people advising to not use git rebase in a public branch, but I can't see what's the problem, if you always rebase a feature branch.

My team always uses branches for features (wow), we are used to have it just locally, so rebase isn't a problem, but sometimes we wanna show the code of a partially done feature to another developer, so we just publicize it, but then we lose all of the advantages of git rebase, or at least that's what you can read at the web.

I don't understand what's the problem, if the developers working at the same public branch never merge it to any branch (when there is still development on that branch) and when they pull it, they do it with a rebase operation. Any changes made to the branch will be always rebase on top of the remote branch, so never lost and you won't have the problem of having the same commit duplicated.

Append 1:

None of the answers until now showed the problems that will happen and how it will happen, so I will try to be clearer.

I will give an example of a workflow using rebase (badly described in the previously paragraphs, sorry) that I don't see any problem.

Initial state:

master         ==========A
origin/feature           +=====AB
feature user A           +=====AB
feature user B           +=====AB

master get a few commits and user A does a few commits:

master         ==========A=====C
origin/feature           +=====AB
feature user A           +=====AB====D
feature user B           +=====AB

User A does a git pull --rebase (he always does it) to update his branch, nothing new comes, then he rebase to master and push:

master         ==========A=====C
origin/feature                 +=====ACB'=====ACB'D
feature user A                 +=====ACB'=====ACB'D
feature user B           +=====AB

(notice the B' is the new commits that still represents the changes B)

Then user B does a few commits:

master         ==========A=====C
origin/feature                 +=====ACB'=====ACB'D
feature user A                 +=====ACB'=====ACB'D
feature user B           +=====AB======E

User B finally does a git pull --rebase as always, there is no need to rebase on master, so he just pushes:

master         ==========A=====C
origin/feature                 +=====ACB'=====ACB'D======E'
feature user A                 +=====ACB'=====ACB'D
feature user B                 +=====ACB'=====ACB'D======E'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱*していゐ 2024-08-25 01:33:43

如果你变基,你就重写了历史。就像在现实世界中一样,如果你想重写历史,你需要一个阴谋:每个人都必须“参与”阴谋(至少是每个了解历史的人,即每个曾经从分支中撤出的人) 。

只要从分支中拉出的人的圈子受到严格控制,阴谋就很容易进行,但是,一旦你发布了这段历史,它就变得更加困难。不过,这并非不可能:例如,Junio C Hamano 的 Git 存储库中的 pu 分支在每次发布后都会重新设置基础,这是一个广泛发布的存储库。其工作原理是,分支将频繁重新基化以及发生这种情况的时间,在 Git 网站、Git wiki 和 Git 邮件列表上都有广泛记录,并且每次重新基化都会提前在邮件列表上宣布,因此人们可以为此做好准备。

If you rebase, you rewrite history. And just like in the real world, if you want to rewrite history, you need a conspiracy: everybody has to be "in" on the conspiracy (at least everybody who knows about the history, i.e. everybody who has ever pulled from the branch).

As long as the circle of people who pull from the branch is tightly controlled, it is fairly easy to get a conspiracy going, however, as soon as you publish that history, it becomes a lot harder. It's not impossible, though: the pu branch in Junio C Hamano's Git repository for example, gets rebased after every release, and that is a widely published repository. The way this works is that the fact that the branch will be frequently rebased and the times when that will happen, are widely documented on the Git website, the Git wiki and the Git mailinglist, and every rebase is announced on the mailinglist in advance so that people can prepare for it.

寂寞清仓 2024-08-25 01:33:43

当您针对公共分支进行变基时,这是完全可以的。

但是,当您对公共分支本身进行变基时,这对于也在使用它的人来说并不好。

补充:

当 rebase 破坏了 unittest 时,您将没有机会 git bisect 有问题的版本。

更详细地说:

  • 您已经准备了一些要添加到分支的代码。
  • 所有单元测试
  • 你已经调试了它,所以它通过了你在(远程)分支中 git-fetched 新更改的
  • ,现在你正在针对重新基化的远程分支重新调整你的代码
  • ,这里单元测试被破坏了
  • 你正在运行 git bisect 并且它指向远程变基。
  • 你的行动?

When you rebase against public branch, it's perfectly ok.

But when you rebase public branch itself, it is not good for people which are also working with it.

Addition:

When rebase breaks unittest, you won't have an opportunity to git bisect the faulty revision.

In more details:

  • you have prepared some code to be added to the branch.
  • you have debugged it so it passes all unit tests
  • you've git-fetched new changes in the (remote) branch
  • now you're rebasing your code against rebased remote branch
  • and here unittests get broken
  • you're running git bisect and it points at the remote rebase.
  • your actions?
亢潮 2024-08-25 01:33:43

http://git-scm.com/book/ch3-6.html

在“变基的危险”部分下

...你会被朋友和家人嘲笑。

http://git-scm.com/book/ch3-6.html

Under the section called "Perils of rebasing"

...you’ll be scorned by friends and family.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文