简体   繁体   English

Git - 功能分支之间的冲突 - 如何避免功能分支包含另一个功能分支中的更改

[英]Git - conflicts between feature branches - how to avoid a feature branch contains changes in another feature branch

The scenarios is as following.场景如下。 I will use t1,t2,t3 and etc. to represent different time:我将使用 t1、t2、t3 等来表示不同的时间:

I have two branches to represent my environments DEV branch, MASTER branch.我有两个分支代表我的环境 DEV 分支,MASTER 分支。

t1: I created a Feature_001 branch from MASTER t1:我从 MASTER 创建了一个 Feature_001 分支

t2: I added commits in the Feature_001 branch and merged my code into DEV and pushed it. t2:我在 Feature_001 分支中添加了提交并将我的代码合并到 DEV 中并推送了它。

t3: for some reason, my manager told me to stop the development of the Feature_001 branch t3:出于某种原因,我的经理告诉我停止 Feature_001 分支的开发

t4: One month passed. t4:一个月过去了。 My colleague Clair created a Feature_002 branch from MASTER.我的同事 Clair 从 MASTER 创建了一个 Feature_002 分支。

t5: Clair added commits in the Feature_002 branch and attempted to merge her code into DEV branch and pushed it. t5:Clair 在 Feature_002 分支中添加了提交,并尝试将她的代码合并到 DEV 分支并推送它。 However, a conflict shows up when she pushed.然而,当她推动时,冲突出现了。

t6: Clair then pulled and merged the changes from the DEV branch into her Feature_002 branch ( my problem happens in this moment ). t6:然后 Clair 将 DEV 分支中的更改提取并合并到她的 Feature_002 分支中(我的问题就发生在这一刻)。 She made a new commit to solve the conflict in the Feature_002 branch.她重新提交以解决 Feature_002 分支中的冲突。 After that, Clair merged her code into DEV and pushed.之后,Clair 将她的代码合并到 DEV 中并进行推送。

t7: After testing, Clair's manager says it's OK to merge into MASTER branch now. t7:经过测试,Clair的经理说现在可以合并到MASTER分支了。 So, Clair merged the Feature_002 branch into the MASTER branch.因此,Clair 将 Feature_002 分支合并到 MASTER 分支中。

t8: Although the Feature_002 Clair developed is functioning in the production, the Feature_001 unintentionally shows up in the production, too, because the Feature_002 branch once merged code from DEV to itself to solve conflicts. t8:Clair开发的Feature_002虽然在生产中正常运行,但Feature_001无意中也出现在了生产中,因为Feature_002分支曾将DEV的代码合并到自身来解决冲突。 Our manager was shocked and started to question who dared to let the Feature_001 go out to production??我们的经理震惊了,开始质疑谁敢让 Feature_001 go 投入生产?

t9: meeting and meeting forever to discuss what happened... t9:永远开会开会讨论发生了什么……

If you follow well on the scenario, you can find because of the conflicts between feature branches, Feature_002 branch will include changes in the Feature_001 branch after Clair pulled the code from DEV.如果你跟得上这个场景,你会发现因为特性分支之间的冲突,Feature_002 分支会包含 Clair 从 DEV 拉取代码后 Feature_001 分支的变化。

My question is how to keep two feature branches independently while enabling us to solve conflicts between them ?我的问题是如何保持两个功能分支独立,同时使我们能够解决它们之间的冲突

Any feedback and discussion is much appreciated.非常感谢任何反馈和讨论。

EDIT 20180925:编辑 20180925:

I want to adjust the situation a bit.我想稍微调整一下情况。 Feature_001 branch can be unwanted or just be under development for a long time. Feature_001 分支可能不需要,也可能已经开发了很长时间。 Let's make it be under development for a long time while Feature_002 was tested first and merged into MASTER real quick.让我们让它长期处于开发状态,而 Feature_002 首先经过测试并快速合并到 MASTER 中。 However, now, again, MASTER branch have both Feature_001 and Feature_002 changes there when we don't want Feature_001 to be in production.但是,现在,当我们不希望 Feature_001 投入生产时,MASTER 分支同时对 Feature_001 和 Feature_002 进行了更改。

I see a number of problems there. 我在那里看到了许多问题。 If feature 001 was not to be developed anymore.... even discarded, say, it should have been taken out of dev branch. 如果不再开发功能001 ...,甚至丢弃,比如说它应该已经从dev分支中删除了。 Given that it wasn't, when you merge dev (through feature 002) onto master (yet another problem, my guess is that this shouldn't happen) then, because feature 001 hadn't been taken out, it landed in master. 假设不是,当您将开发(通过功能002)合并到主服务器上时(还有另一个问题,我猜这是不应该发生的),然后由于功能001没有被取出,它就落入了主服务器中。

So.... how to avoid it? 那么...如何避免呢? Everybody will say a different thing. 每个人都会说不同的话。 My take? 我拿 When you got the notice that feature 0001 was to be stopped, it should have been taken out of dev branch (either by rewriting dev history to avoid the merge or by reverting 0001 revisions). 当您收到功能0001即将停止的通知时,它应该已从dev分支中删除(通过重写dev历史记录以避免合并或还原0001版本)。 And then, I guess you shouldn't merge from dev into master... but that's just a guess cause it really depends on your workflow. 然后,我猜您不应该将开发人员合并到主人员中……但这只是一个猜测,因为它实际上取决于您的工作流程。

If the changes from Feature_001 weren't intended to be released to production, it shouldn't have been merged to DEV. 如果Feature_001的更改不打算发布到生产环境,则不应将其合并到DEV。 The changes should have been left on the Feature_001 branch. 所做的更改应留在Feature_001分支上。 If the decision to not release Feature_001 was made after it was merged to development, the changes should have been reverted. 如果在合并到开发中后做出不发布Feature_001的决定,则更改应已还原。 As long as it is present on DEV, other users who pull from DEV will have the changes in their branch. 只要它存在于DEV上,其他从DEV提取的用户都将在其分支中进行更改。

The branch Feature_001 shouldn't have merged to DEV until Feature_001 was completed and pull request approved. 在Feature_001完成并批准拉取请求之前,分支Feature_001不应该合并到DEV。 The conflicts will have to be resolved once Feature_001 is merged into DEV not before, this will avoid the problem you encountered, where Feature_002 branch had some commited code from Feature_001. 一旦将Feature_001合并到DEV中,就必须立即解决冲突,这将避免您遇到的问题,Feature_002分支具有来自Feature_001的某些提交的代码。 Ideally the Feature_001 should either be small or broken down into smaller features such as Feature-001-S-xxxxxx-MyStoryDescription for easy tracking. 理想情况下,Feature_001应该较小或分解为较小的特征,例如Feature-001-S-xxxxxx-MyStoryDe​​scription,以便于跟踪。 Additionally, since your branch Feature-001 might have many commits, it is recommended to squash your commits before doing pull request and rebase your branch when a merge conflicts occurs. 此外,由于您的分支Feature-001可能有很多提交,因此建议在执行拉取请求之前先压缩提交,并在发生合并冲突时重新设置分支的基础。 Happy coding! 编码愉快!

I understand that you are not using GitFlow, because you created your feature branches since master (in Gitflow it should be develop), and you are pushing changes not ready to be released in develop (in GitFlow, the release branch is created since develop).我知道你没有使用 GitFlow,因为你从 master 开始创建了你的功能分支(在 Gitflow 中它应该是 develop),并且你正在推送尚未准备好在 develop 中发布的更改(在 GitFlow 中,发布分支是在 develop 中创建的) .

Hence, thinking in your actual flow, to avoid these issues we can:因此,在您的实际流程中思考,为避免这些问题,我们可以:

  1. Avoid to push changes not ready to production to develop.避免将尚未准备好进行生产的更改推向开发。 Test your code in your local machine.在本地机器上测试您的代码。 If you really need to test your code in some specific environment, "borrow" the environment and deploy your feature branch only.如果您确实需要在某个特定环境中测试您的代码,请“借用”该环境并仅部署您的功能分支。

  2. Create features that only works if some flag is activated, for example a flag FEATURE_ACTIVE in db.创建仅在某些标志被激活时才有效的功能,例如 db 中的标志 FEATURE_ACTIVE。 It should have the value Y in qa (to test) and N in production (to avoid issues).它在 qa(测试)中应具有值 Y,在生产中应具有值 N(以避免问题)。

  3. NOT RECOMENDABLE.不推荐。 If for some reason, you still need to push changes not ready for production to develop.如果出于某种原因,您仍然需要推动尚未准备好进行生产的更改。 When you face a conflict, you can create a temporary branch that solves the conflict between two branches and then, merge it to develop.当你遇到冲突时,你可以创建一个临时分支来解决两个分支之间的冲突,然后合并它来开发。 This allows you to maintain the branches independently.这允许您独立维护分支。 The author of the branch that will be deployed later, should be aware of the merge of the other branch to the branch master, after that he can pull the changes from master and also merge the temporary branch into it to keep the branches clean.稍后将部署的分支的作者应该知道另一个分支合并到分支 master,之后他可以从 master 拉取更改并将临时分支合并到其中以保持分支清洁。 For example in this case, your friend should have created a new branch since Feature_002 called Feature_001_002_temp and then merge the branch Feature_001 into it, solve the conflicts and merge it to develop.例如在这种情况下,您的朋友应该从 Feature_002 开始创建一个名为 Feature_001_002_temp 的新分支,然后将分支 Feature_001 合并到其中,解决冲突并合并它以进行开发。 Any additional change to Feature_002 should pass throug this temporary branch until Feature_002 goes to prod.对 Feature_002 的任何其他更改都应通过此临时分支,直到 Feature_002 进入生产状态。 Be very careful with the changes, some conflicts such as deletions, values of constants could break your app.对更改要非常小心,一些冲突(例如删除、常量值)可能会破坏您的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Git - 当功能分支包含大量更改时,如何在将功能分支合并到开发时避免冲突 - Git - how to avoid conflict when merging feature branch to dev when feature branch contains lots of changes 2 个分支之间的 Git 差异仅显示在功能分支中所做的更改 - Git difference between 2 branches show changes made in feature branch only 如何将更改从一个功能分支复制到从 GIT 中的同一父分支派生的另一个功能分支 - How to copy changes from one feature branch to another feature branch derived from same parent branch in GIT Git - 在开发人员之间将一个功能分支的更改合并到另一个功能分支 - Git - Merging changes from one feature branch into another between developers 预测功能分支在主分支中合并时的合并冲突 - Predicting merge conflicts between feature branches when they are merged in the master branch 回滚功能分支git中的更改 - Rollback changes in feature branch git Git 在解决冲突时,变基未显示功能/本地分支更改 - Git Rebasing is not showing feature/local branch changes while resolving conflicts 如何将分支分成功能分支 - How to separate a branch into feature branches 具有许多功能分支的故事分支的Git模型 - Git model for story branch with many feature branches Git将功能分支转移到另一个分支 - Git rebase feature branch to another branch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM