如何在不包含之前提交的所有其他分支中导入master的bug修复?

时间:2023-02-05 14:10:40

I have some branches: branch 1 and branch 2. They have their origin from the master branch. Now there were some changes in the master branch. One is a critical bugfix, which has to be imported on branch 1 and 2. But there is a change (or perhaps multiple) which should not be imported on the other branches.

我有一些分支:分支1和分支2.它们来自主分支。现在主分支发生了一些变化。一个是关键的错误修正,必须在分支1和2上导入。但是有一个更改(或者可能是多个)不应该在其他分支上导入。

How is that solved in the GIT world?

如何在GIT世界中解决这个问题?

如何在不包含之前提交的所有其他分支中导入master的bug修复?

One idea I came up with is to create the bugfix branch before the other changes are made to the master branch. This would work if you go back in history and you know that at that point the branches 1 and 2 are using the same code base as in the master branch.

我想出的一个想法是在对主分支进行其他更改之前创建bugfix分支。如果您回到历史记录中并且您知道此时分支1和2使用与主分支中相同的代码库,则此功能将起作用。

What if branch 1 and 2 contain different versions of the master, but the affected code is the same?

如果分支1和2包含不同版本的主服务器,但受影响的代码是相同的,该怎么办?

branch 1: master v1.0
branch 2: master v1.1

分支1:主v1.0分支2:主v1.1

I can't merge the latest version of the master into the other branches, because there are changes, which the branches shouldn't get yet. Should the changes be made in every branch?

我无法将最新版本的master合并到其他分支中,因为有些更改,分支不应该得到。是否应该在每个分支进行更改?

3 个解决方案

#1


2  

Merge instead of cherry-pick!

合并而不是樱桃挑选!

cherry-pick makes a new commit on your branch, and that commit doesn't not refer to where the bugfix was originally created, hiding actual history. Furthermore, the bugfix could be several commits, or have further development in the future (like, it didn't turn out to really fix it the first time). This makes cherry-pick inconvenient.

cherry-pick在你的分支上进行一次新的提交,并且提交并不是指最初创建bug修复的位置,隐藏实际的历史记录。此外,错误修复可能是几个提交,或将来有进一步的发展(比如,它并没有真正修复它第一次)。这使樱桃挑选不方便。

I'd much rather merge the bugfix branch. When you created the bugfix commit on master, surely you had a branch there?

我宁愿合并bugfix分支。当你在master上创建bugfix提交时,你肯定有一个分支吗?

$ git checkout master
$ git checkout -b bugfix_1234
... work work work
$ git commit -a -m"Fix the bug blablabla"
... oh snap, I forgot to clean blublu. Work work work
$ git commit -a -m"Clean blublu"
... test, it's fine, merge to master
$ git checkout master
$ git merge bugfix_1234 --no-ff
... mmm, my branches 1 and 2 also need the change
$ git checkout branch_1
$ git merge bugfix_1234
$ git checkout branch_2
$ git merge bugfix_1234

Done! The nice thing with this is that you don't have duplicate commits doing the same thing. Your history reflects also what you actually did, work on a branch, integrate its changes to other branches.

完成!这样做的好处是你没有重复的提交做同样的事情。您的历史记录还反映了您实际执行的操作,在分支机构上工作,将其更改集成到其他分支机构。

If you don't have the branch bugfix_1234 anymore, you can always recreate one, or an annotated tag.

如果您不再拥有分支bugfix_1234,则始终可以重新创建一个或带注释的标记。

Look at the red dot in this git workflow, it's exactly what it is doing.

看看这个git工作流程中的红点,它正是它正在做的事情。

Edit: as per your comment in another answer, you need your bugfix (hotfix in the workflow) to be based on a common ancestor of the branches and master. If the fix applies to both the branches and master, it makes sense anyway.

编辑:根据您在另一个答案中的评论,您需要您的错误修正(工作流中的修补程序)基于分支和主人的共同祖先。如果修复适用于分支和主分支,那么无论如何它都是有意义的。

#2


1  

Your idea to branch off of master for the bug fix is a good one. After the fix, you will want to merge, not cherry-pick, bug into the different branches:

你想从错误修复程序中分离出主人的想法很好。修复之后,你会想要合并而不是挑选bug到不同的分支:

git checkout branch1
git merge bug
git checkout branch2
git merge bug
git checkout master
git merge bug

This way, when you merge master into one of the other branches later, git will know what has already been merged in. If you did a cherry-pick instead, git would not know where those changes came from, and you might get conflicts down the road.

这样,当你稍后将master合并到其他一个分支中时,git就会知道已经合并了什么。如果你做了一个樱桃选择,git就不知道这些变化来自哪里,你可能会发生冲突马路。

By the way, you may want to look into Git flow; it addresses these kinds of issues nicely.

顺便说一下,你可能想看看Git流程;它很好地解决了这些问题。

#3


0  

You can do like this .

你可以这样做。

Go in the branch you want apply the commit to.

进入要应用提交的分支。

git checkout branch-name

Execute the following git command:

执行以下git命令:

git cherry-pick <commit-hash>

#1


2  

Merge instead of cherry-pick!

合并而不是樱桃挑选!

cherry-pick makes a new commit on your branch, and that commit doesn't not refer to where the bugfix was originally created, hiding actual history. Furthermore, the bugfix could be several commits, or have further development in the future (like, it didn't turn out to really fix it the first time). This makes cherry-pick inconvenient.

cherry-pick在你的分支上进行一次新的提交,并且提交并不是指最初创建bug修复的位置,隐藏实际的历史记录。此外,错误修复可能是几个提交,或将来有进一步的发展(比如,它并没有真正修复它第一次)。这使樱桃挑选不方便。

I'd much rather merge the bugfix branch. When you created the bugfix commit on master, surely you had a branch there?

我宁愿合并bugfix分支。当你在master上创建bugfix提交时,你肯定有一个分支吗?

$ git checkout master
$ git checkout -b bugfix_1234
... work work work
$ git commit -a -m"Fix the bug blablabla"
... oh snap, I forgot to clean blublu. Work work work
$ git commit -a -m"Clean blublu"
... test, it's fine, merge to master
$ git checkout master
$ git merge bugfix_1234 --no-ff
... mmm, my branches 1 and 2 also need the change
$ git checkout branch_1
$ git merge bugfix_1234
$ git checkout branch_2
$ git merge bugfix_1234

Done! The nice thing with this is that you don't have duplicate commits doing the same thing. Your history reflects also what you actually did, work on a branch, integrate its changes to other branches.

完成!这样做的好处是你没有重复的提交做同样的事情。您的历史记录还反映了您实际执行的操作,在分支机构上工作,将其更改集成到其他分支机构。

If you don't have the branch bugfix_1234 anymore, you can always recreate one, or an annotated tag.

如果您不再拥有分支bugfix_1234,则始终可以重新创建一个或带注释的标记。

Look at the red dot in this git workflow, it's exactly what it is doing.

看看这个git工作流程中的红点,它正是它正在做的事情。

Edit: as per your comment in another answer, you need your bugfix (hotfix in the workflow) to be based on a common ancestor of the branches and master. If the fix applies to both the branches and master, it makes sense anyway.

编辑:根据您在另一个答案中的评论,您需要您的错误修正(工作流中的修补程序)基于分支和主人的共同祖先。如果修复适用于分支和主分支,那么无论如何它都是有意义的。

#2


1  

Your idea to branch off of master for the bug fix is a good one. After the fix, you will want to merge, not cherry-pick, bug into the different branches:

你想从错误修复程序中分离出主人的想法很好。修复之后,你会想要合并而不是挑选bug到不同的分支:

git checkout branch1
git merge bug
git checkout branch2
git merge bug
git checkout master
git merge bug

This way, when you merge master into one of the other branches later, git will know what has already been merged in. If you did a cherry-pick instead, git would not know where those changes came from, and you might get conflicts down the road.

这样,当你稍后将master合并到其他一个分支中时,git就会知道已经合并了什么。如果你做了一个樱桃选择,git就不知道这些变化来自哪里,你可能会发生冲突马路。

By the way, you may want to look into Git flow; it addresses these kinds of issues nicely.

顺便说一下,你可能想看看Git流程;它很好地解决了这些问题。

#3


0  

You can do like this .

你可以这样做。

Go in the branch you want apply the commit to.

进入要应用提交的分支。

git checkout branch-name

Execute the following git command:

执行以下git命令:

git cherry-pick <commit-hash>