git 当开发分支与主分支非常不同时管理修补程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7175869/
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-10 11:40:56  来源:igfitidea点击:

Managing hotfixes when develop branch is very different from master?

gitgit-flow

提问by TaylorOtwell

I'm using the "Git Flow" branching model, with a master branch and a develop branch. I'm working on a major new release, so my develop branch is wildly different from my master branch. This creates a problem anytime I need to make a hotfix on the master branch and merge it back into develop. There are almost always conflicts, and it's becoming a real pain.

我正在使用“Git Flow”分支模型,有一个主分支和一个开发分支。我正在开发一个主要的新版本,所以我的开发分支与我的主分支大不相同。每当我需要在 master 分支上进行修补程序并将其合并回 develop 时,这都会产生问题。几乎总是有冲突,这正在成为一种真正的痛苦。

What is the best way to manage this? It would be easier for me to make the small hotfix changes on develop manually and then merge everything into master when I'm ready without merging master back into develop. Is this possible?

管理这种情况的最佳方法是什么?对我来说,手动在开发上进行小的修补程序更改会更容易,然后在我准备好时将所有内容合并到母版中,而无需将母版合并回开发版。这可能吗?

回答by eckes

The simplest way to get somecommits from one branch to another is cherry-picking.

从一个分支获取一些提交到另一个分支的最简单的方法是cherry-picking

Assuming that your fix in masterhas the commit hash HASHand you want to take that hotfix into your develbranch, do a git checkout develfollowed by a git cherry-pick HASH. That's it.

假设您的修复程序master具有提交哈希HASH并且您想将该修补程序带入您的devel分支,请执行 agit checkout devel后跟git cherry-pick HASH. 就是这样。

If you want to take allchanges from masterinto devel, you can achieve that with

如果您想将所有更改从masterinto 中取出devel,您可以使用

git checkout devel
git rebase master


If you have the opposite scenario (you make a hotfix during development in a develbranch and want to take that fix into masterbefore develgets fully merged into master), the workflow is quite similar. Assuming that the hotfix has the hash HOTFIX_HASH, do this:

如果您有相反的情况(您在devel分支的开发过程中制作了一个修补程序,并希望masterdevel完全合并到之前将该修补程序纳入master),则工作流程非常相似。假设修补程序具有 hash HOTFIX_HASH,请执行以下操作:

git checkout master
git cherry-pick HOTFIX_HASH

Now, the commit is present in masterand devel. To get around this, type

现在,提交存在于masterand 中devel。要解决此问题,请键入

git checkout devel
git rebase master

and the commit will disappear from develsince it's already present in master.

并且提交将从中消失,devel因为它已经存在于master.

回答by tswicegood

My general workflow for this situation is to create a bug-fixbranch of masterthat fixes the problem. Once it's ready, merge that back into masterthen merge masterinto develop.

我针对这种情况的一般工作流程是创建一个bug-fix分支master来解决问题。准备好后,将其合并回master然后合并masterdevelop.

This assumes that your bug fix is almost a one-to-one between the code it needs to change in both branches. If that's the case, you could always try a git merge -s ours master(see man-page) into developso the developbranch takes priority.

这假设您的错误修复在两个分支中需要更改的代码之间几乎是一对一的。如果是这种情况,您总是可以尝试 a git merge -s ours master(请参阅手册页),develop以便develop分支优先。

I use a similar process for managing bug fix releases on an open-source project I'm working on. masteris always ahead of where the bug fix needs to be applied, so I create a branch from the tag that needs the fix, apply the fix and release, then retag and merge the new tag into master. This causes a conflict because of the version numbers, but can be avoided with the command above.

我使用类似的过程来管理我正在处理的开源项目上的错误修复版本。 master总是在需要应用错误修复的地方之前,所以我从需要修复的标记创建一个分支,应用修复和发布,然后重新标记并将新标记合并到master. 由于版本号,这会导致冲突,但可以使用上面的命令来避免。

Hope that helps.

希望有帮助。

回答by Cristian Douce

I usually follow this guidewhich fits quite well in most cases and avoids mayor of issues with conflicts and bigchanges.

我通常遵循这个指南,它在大多数情况下都非常适合,并避免了冲突和重大变化的问题。

If you could work on featurebranches and merge them in developmentonly prior to a releasebranch creation (meaning you are actually preparing a release)... this method should avoid most of the merge conflicts you experience.

如果您可以在feature分支上工作并development仅在release创建分支之前合并它们(意味着您实际上是在准备发布)……这种方法应该可以避免您遇到的大多数合并冲突。

Since breaking changes would occur at a feature-breakingbranch, you MAY only have conflicts once at the time this feature-breakingbranch gets merged into development. And you could as well merge developmentinto the releasebranch at any time to keep it updated.

由于feature-breaking分支会发生重大更改,因此在此feature-breaking分支合并到开发中时,您可能只会发生一次冲突。您也可以随时合并developmentrelease分支中以保持更新。

You will also be cool with merging into developmentall the hotfix-branches you have with minimum or non conflicts at all.

你也会很酷地合并到你拥有的development所有hotfix-branches 中,而冲突最少或根本没有冲突。

The guide I shared on the link before makes big emphasis on never merging from developmentto masteror backwards. Always handle your releases via a releasebranch.

我的链接共享的指南之前,使大强调从永远合并developmentmaster或后退。始终通过release分支处理您的发布。