简体   繁体   English

用另一个分支的更改覆盖分支

[英]Overwrite branch with changes from another branch

Hello fellow git users!各位git用户您好!

I am having the following problem and I don't know what would be the best and correct way to avoid it.我遇到以下问题,我不知道避免它的最佳和正确方法是什么。 So here I will explain:所以在这里我将解释:

I am working with 2 different branches:我正在与 2 个不同的分支机构合作:

  • master --> production master --> 生产

  • dev --> development dev --> 开发

The workflow I am currently using is the following:我目前使用的工作流程如下:

  1. Changes are made on dev branchdev分支上进行更改
  2. Changes are tested and approved更改经过测试和批准
  3. Checkout master and merge dev to include new changes Checkout master并合并dev以包含新的更改

Nothing crazy so far, right?到目前为止没有什么疯狂的,对吧?

Now, the following scenario appeared a few weeks ago:现在,几周前出现了以下场景:

  1. Changes are made on dev branchdev分支上进行更改

  2. Changes are tested and approved更改经过测试和批准

  3. Checkout master and merge dev to include new changes Checkout master并合并dev以包含新的更改

  4. Someone realized part of those changes should not be in master (production)有人意识到这些更改的一部分不应该在master (生产)中

  5. In master branch we commented those changes so that those are not enabled in mastermaster分支中,我们评论了这些更改,以便在master中不启用这些更改

Current situation:现在的情况:

  • All changes are in dev所有更改都在dev

  • All changes are in master but part of them are disabled所有更改都在master中,但其中一部分已禁用

  • So far so good到目前为止,一切都很好

A few days later, the following happened:几天后,发生了以下事情:

  1. A few more changes were added to devdev添加了一些更改

  2. Changes were tested and approved更改已经过测试和批准

  3. Checkout master and merge dev to include recent new changes and also part of the change that is now commented in master .检查master并合并dev以包括最近的新更改以及现在在master中注释的更改的一部分。

  4. As a result, everything is included in master but the change that had been commented is still commented in master .结果,所有内容都包含在master中,已评论的更改仍在master中评论。

At the beginning, I was kind of expecting master to be overwritten with the dev version but then I realised that does not make much sense as merging is actually putting together changes from both branches so the result is making absolute sense.一开始,我有点期待masterdev版本覆盖,但后来我意识到这没有多大意义,因为合并实际上是将两个分支的更改放在一起,所以结果是绝对有意义的。 However, it is not what I need.但是,这不是我需要的。

What would be the best solution for this?最好的解决方案是什么? I was thinking about the following options:我在考虑以下选项:

  1. To absolutely prohibit making changes in master branch.绝对禁止在master分支中进行更改。 So any change (like commenting part of the code) should be done first in dev and then go to master .所以任何更改(比如注释部分代码)都应该首先在dev中完成,然后 go 到master

  2. When merging into master to use a parameter saying ignore all changes in the current branch and just take all that is coming from dev branch .合并到master时使用参数表示忽略当前分支中的所有更改,只获取来自dev分支的所有内容 That would be awesome and obviously I don't know how to do it in git.那太棒了,显然我不知道如何在 git 中做到这一点。

Anyway, your comments will be much appreciated.无论如何,您的意见将不胜感激。

Thanks!谢谢!

This is what you should have done这是你应该做的

You should have merged your master into dev (a sort of hotfix, which are fine) right after you made the changes to master .在对master进行更改后,您应该立即将master合并到dev (一种修补程序,这很好)。

Then on dev you should have reverted the commit ( git revert commit-id ) which commented out things in master .然后在dev上你应该恢复提交git revert commit-id ),它在master中注释掉了东西。

Then when you went on to merge dev into master again, things would be fine.然后当你继续将dev再次合并到master时,一切都会好起来的。

Now that things have gone further you can:现在事情已经更进一步了,你可以:

  1. [BEST APPROACH] try those steps above; [最佳方法] 尝试上述步骤; if changes made in dev aren't deep enough and/or don't touch the same places as the commit , chances are the all is going to be right;如果在dev中所做的更改不够深入和/或没有触及与commit相同的地方,那么一切都会是正确的; or要么

  2. in master branch go on to revert ( git revert ) the commit .master分支 go 上还原( git revert提交

Please read up on GitFlow, at Vincent Driessen's original blog about it .请在 GitFlow 上阅读Vincent Driessen 的原始博客

What's missing here is that you made commits directly to master .这里缺少的是你直接提交给master Never do that: :)永远不要那样做::)

You have a few options, depending on your exact situation, but if you are following GitFlow, here would be the answer:您有几个选项,具体取决于您的具体情况,但如果您遵循 GitFlow,答案如下:

  1. When ready for a release, make a release_xxx branch, which is essentially a release candidate for a new version.当准备好发布时,创建一个release_xxx分支,它本质上是新版本的发布候选者。
  2. Use the release branch during integration and system testing with other repos/code, etc.在与其他回购/代码等的集成和系统测试期间使用发布分支。
  3. If a change is required, make it to release , but then merge that change to dev as well.如果需要更改,请将其release ,然后将该更改也合并到dev
  4. When release is fully tested, merge it into master, ending it.release完全测试后,将其合并到 master 中,结束它。

If you don't normally need any extended time for a release branch, you can create a hotfix branch, which, after testing, would be merged both to master and dev .如果您通常不需要延长发布分支的时间,则可以创建一个hotfix分支,该分支在测试后将合并到masterdev Hotfix branches are normally for production problems from something already released into production - it's like a feature branch that merges into dev , except intended to merge directly back to master (in addition to dev so all the development branches get the change as well). Hotfix 分支通常用于解决已经发布到生产环境中的生产问题——它就像一个合并到dev的功能分支,除了打算直接合并回master (除了dev之外,所以所有开发分支也会得到更改)。

This allows you to continue with other parallel development in dev without having to push new things put in dev to master , leaving just the changes needed for the fix to happen in a release or hotfix branch, and you can manage pushing that back to dev separately to make sure it incorporates with any parallel work.这使您可以继续在dev中进行其他并行开发,而不必将dev中的新内容推送到master ,只留下修复所需的更改发生在releasehotfix分支中,并且您可以单独将其推送回dev以确保它与任何并行工作相结合。

在此处输入图像描述

Credit: Vincent Driessen图片来源:文森特·德里森


To resolve the current issue解决当前问题

You should reverse-merge master into dev , resolve all differences (ie add or remove comments, etc.), and then merge back to master .您应该将master反向合并到dev ,解决所有差异(即添加或删除评论等),然后合并回master If you just want to cleanup, you can do so and direct push to master, but it's better to have a PR with review of peers in dev to give traceability and shows clearly what was done in the git history.如果你只是想清理,你可以这样做并直接推送到 master,但最好有一个 PR 和dev中同行的审查,以提供可追溯性并清楚地显示 git 历史中做了什么。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM