[英]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:我目前使用的工作流程如下:
dev
branch在dev
分支上进行更改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:现在,几周前出现了以下场景:
Changes are made on dev
branch在dev
分支上进行更改
Changes are tested and approved更改经过测试和批准
Checkout master
and merge dev
to include new changes Checkout master
并合并dev
以包含新的更改
Someone realized part of those changes should not be in master
(production)有人意识到这些更改的一部分不应该在master
(生产)中
In master
branch we commented those changes so that those are not enabled in master
在master
分支中,我们评论了这些更改,以便在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:几天后,发生了以下事情:
A few more changes were added to dev
向dev
添加了一些更改
Changes were tested and approved更改已经过测试和批准
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
中注释的更改的一部分。
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.一开始,我有点期待master
被dev
版本覆盖,但后来我意识到这没有多大意义,因为合并实际上是将两个分支的更改放在一起,所以结果是绝对有意义的。 However, it is not what I need.但是,这不是我需要的。
What would be the best solution for this?最好的解决方案是什么? I was thinking about the following options:我在考虑以下选项:
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
。
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!谢谢!
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
时,一切都会好起来的。
[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要么
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,答案如下:
release_xxx
branch, which is essentially a release candidate for a new version.当准备好发布时,创建一个release_xxx
分支,它本质上是新版本的发布候选者。release
, but then merge that change to dev
as well.如果需要更改,请将其release
,然后将该更改也合并到dev
。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
分支,该分支在测试后将合并到master
和dev
。 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
,只留下修复所需的更改发生在release
或hotfix
分支中,并且您可以单独将其推送回dev
以确保它与任何并行工作相结合。
Credit: Vincent Driessen图片来源:文森特·德里森
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.