如何维护长期运行的 git 分支
理想情况下,Git 分支应该持续很短的时间,可能是 1-2 天。然后它被合并到一些主线中。
但在某些情况下,当我们处理非常大的功能时,我们会维护分支。当 2 或 3 个人在专有代码区域中分别开发这些非常大的功能时,维护它们就变得有点困难。
在进入稳定分支的热修复中,我们需要使这 2-3 个大分支与稳定分支保持同步。所以我们最终经常这样做。
(in feature-branch1) $ git merge stable
(in feature-branch2) $ git merge stable
(in feature-branch3) $ git merge stable
有没有正确的方法来维护 git 中这些长期运行的分支?通过执行上述操作,git 历史记录有点混乱。这些功能分支大多被推送到远程,这意味着我们看不到rebase
作为一个选项。我还能做什么?
Git branches ideally should last for short time, probably for 1-2 days. Then it gets merged into some mainline.
But in some cases, where we work on very big features, we maintain branches. And when 2 or 3 people each work on these very big features in exclusive areas of code, it becomes kinda tough to maintain them.
Amidst hot fixes that go to stable branch, we need to keep these 2-3 big branches in sync with the stable branch. So we end up doing this, quite often.
(in feature-branch1) $ git merge stable
(in feature-branch2) $ git merge stable
(in feature-branch3) $ git merge stable
Is there a right way to maintain these long running branches in git? By doing the above, the git history is kind of messy. These feature branches mostly get pushed to a remote which means that we cannot see rebase
as an option. What else can I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
时不时地合并到稳定分支实际上是保持功能分支最新的最简单也是最好的事情。我不建议重新设置基准,因为可能有更多的人同时处理该功能,并且每次强制推送时他们都必须重置本地分支。即使只有一个人在功能分支上工作,合并也会更好,因为它清楚地显示了您从稳定分支引入修复的历史点。
是的,一旦将功能分支合并回稳定分支,您的历史记录可能会看起来有点混乱。但是,只要您避免 git pull 中日常微合并的混乱(使用 git pull --rebase 代替),您将能够欣赏到真正有意义的合并。
如果您确实想要避免将新功能从稳定版合并到功能分支中,但只想修复错误,则可以将错误修复挑选到功能分支中。然而,这可能需要大量工作,因为它要求您始终掌握稳定中发生的所有事情。使用 gitcherry 来帮助你:
Merging in the stable branch every now and then is actually the easiest and best thing you can do to keep your feature branch up to date. I wouldn't recommend rebasing because there might be more people working on the feature at the same time and they would have to reset their local branches a lot every time there was a forced push. Even if only one person works on the feature branch, merging is better because it clearly shows the point in history where you brought in fixes from the stable branch.
Yes, your history can look kind of messy once you merge the feature branch back into stable. But as long as you're avoiding the mess of daily micro-merges from your git pulls (use
git pull --rebase
instead), you will be able to appreciate actual meaningful merges.If you really want to avoid merging in new features from stable into the feature branch, but just want bugfixes, you can cherry-pick bugfixes into the feature branch. However this can be a lot of work because it requires that you're always on top of everything that's happening in stable. Use
git cherry
to help you out:如果团队足够小并且由相当有经验的 git 用户组成,则可以进行变基。
例如,您可以做的是同意功能分支每天晚上都将基于稳定版本。或者,您可以在将功能分支变基为稳定版本时发送电子邮件。
一切都应该没问题,但是
如果您忘记与重新定位的功能分支同步,假设您在旧功能分支
oldFB
之上提交了C
,该分支已重新定位为oldFB'< /code>:
仍然可以通过运行以下命令来重新设置提交
C
:请注意,您必须手动查找上图中名为
oldFB
的提交。Rebasing is possible if the team is small enough, and consists of reasonably experienced git users.
What you can do is, for instance, agree that the feature-branch will be rebased on stable every night. Or you could send an email when you rebase the feature-branch to stable.
Everything should be fine, but
in case you forget to sync with the rebased feature branch, say you committed
C
on top of the old feature brancholdFB
, which has been rebased intooldFB'
:It is still possible to rebase your commit(s)
C
by running:Note that you'll have to find manually the commit called
oldFB
in the diagram above.最好的选择是仅将所需的小错误修复/功能分支合并到长期存在的功能分支中。这样你就只能得到你需要的东西。请注意,如果您让这些分支存活足够长的时间,它们最终可能会与主分支完全不同步。有时,您可能希望从长期存在的功能分支创建一个临时分支,并将 master 合并到其中只是为了进行健全性检查 - 查看是否存在冲突,查看 master 中的更改是否会破坏该功能,等等在。
接下来最好的办法是定期将 master 合并到长期存在的功能分支中。这是相当安全的,但也可能会合并许多您不感兴趣的更改,并使事情变得复杂。
我不建议定期重新建立 master 基础。这使得每个人都很难保持同步,也使得在功能分支上维护复杂的历史记录(合并)变得更加困难,并使冲突解决变得更加痛苦。
The best option is to merge only the required small bugfix/feature branches into your long-lived feature branches. This way you only get what you need. Note that if you let these branches live long enough, they could ultimately get fairly out of sync from master. From time to time, you might want to create a temporary branch off of your long-lived feature branch, and merge master into it just to do sanity checks - see if there are conflicts, see if changes in master break the feature, and so on.
Next best would be to periodically merge master into your long-lived feature branches. This is fairly safe, but can also merge in a lot of changes you aren't interested in, and complicate things.
I would not recommend rebasing periodically onto master. It makes it tough for everyone to stay in sync, and it also makes it much more difficult to maintain complex history (merges) on your feature branches, and makes the conflict resolution more of a pain.