Git 合并冲突 |如何管理分支使它们彼此隔离,但也使用彼此依赖的代码
Git merge conflict | how to manage branches to make them isolated from each other but also use dependent code from each other
- 我有一个 master 分支。
- 我从 master 创建了一个名为
Issue1
的分支。完成 Issue1
后,我创建了另一个名为 Issue2
的问题
来自大师,请注意我没有在 Issue1
. 之上创建它
- 第 2 步的原因是,在将问题推送到生产环境时,可能存在我不必合并代码的情况
来自
Issue1
,但如果我在 Issue1
之上创建它,那么在我推送 Issue2
时可能会出现问题,因此从
两个分支都将被推送到生产环境(master)。
- 这就是为什么我从 master 分支创建每个问题以使每个问题彼此隔离。
- 但问题是在处理
Issue1
时我修改了文件并创建了通用方法或其他需要的东西
Issue2
。在那种情况下,我没有来自 Issue1
. 的代码
所以从上面看,在 Issue1
之上创建 Issue2
很好,但是那样我的要求就没有实现它的目的。
我读到关于愉快采摘的文章。不确定这里是否适用。
此外,如果我在 Issue1
中修改了 index.php 并添加了 link 并且在 Issue2
上我修改了同一个文件并添加了另一个 link,
会有冲突。所以我无法阻止它,虽然我知道我已经修改了这个文件 branches.I 必须处理
解决冲突,我不能阻止它,直到我在 Issue1
之上创建它,但在这种情况下我的第二个目的将受到影响。
解决此问题的最佳方法是什么。我希望我的两个目的都得到解决。
考虑创建第 3 个分支,f.e。 Feature1
或 Common1
,并在那里编写通用代码。然后,您可以将此分支合并到 Issue1
和 Issue2
(以及 master,ofc)。
我认为这是非常 git 的方式 - 与问题相关的代码在问题分支中,通用代码在通用分支中。
编辑1:
我提议的最终结构:
master
|
Common
/ \
Issue1 Issue2
如果您已经有问题,它可能看起来像这样:
master
/ | \
Issue1 Common1 Issue2
| / | \ /
Merge | Merge
| | |
Issue1 updated Issue2
common
Edit2:另一种解释。
你有主人:
var x = 1;
var y = 2;
print(x+y);
假设您的 Issue1 应该如下所示:
var x = 1;
var y = 3;
printLine(x+y);
function printLine (x) { return print(x + 'line'); }
您的 Issue2 应如下所示:
var x = 1;
var y = 4;
printLine(x+y);
function printLine (x) { return print(x + 'line'); }
然后你创建 Common1 分支:
var x = 1;
var y = 2;
printLine(x+y);
function printLine (x) { return print(x + 'line'); }
因此您的工作流程将如下所示:
git checkout master
git checkout -b common1
//make changes as in Common1
git commit -am common1
git checkout issue1
git merge common1
git checkout issue2
git merge common1
//then you can work on issues. f.e. making changes in issue1
git checkout issue1
//code, code
git commit -am issue1
//if you want to update common methods
git checkout common1
//update
git commit -am updated-common1
git checkout issue1
git merge common1
git checkout issue2
git merge common2
- 我有一个 master 分支。
- 我从 master 创建了一个名为
Issue1
的分支。完成Issue1
后,我创建了另一个名为Issue2
的问题 来自大师,请注意我没有在Issue1
. 之上创建它
- 第 2 步的原因是,在将问题推送到生产环境时,可能存在我不必合并代码的情况
来自
Issue1
,但如果我在Issue1
之上创建它,那么在我推送Issue2
时可能会出现问题,因此从 两个分支都将被推送到生产环境(master)。 - 这就是为什么我从 master 分支创建每个问题以使每个问题彼此隔离。
- 但问题是在处理
Issue1
时我修改了文件并创建了通用方法或其他需要的东西Issue2
。在那种情况下,我没有来自Issue1
. 的代码
所以从上面看,在 Issue1
之上创建 Issue2
很好,但是那样我的要求就没有实现它的目的。
我读到关于愉快采摘的文章。不确定这里是否适用。
此外,如果我在 Issue1
中修改了 index.php 并添加了 link 并且在 Issue2
上我修改了同一个文件并添加了另一个 link,
会有冲突。所以我无法阻止它,虽然我知道我已经修改了这个文件 branches.I 必须处理
解决冲突,我不能阻止它,直到我在 Issue1
之上创建它,但在这种情况下我的第二个目的将受到影响。
解决此问题的最佳方法是什么。我希望我的两个目的都得到解决。
考虑创建第 3 个分支,f.e。 Feature1
或 Common1
,并在那里编写通用代码。然后,您可以将此分支合并到 Issue1
和 Issue2
(以及 master,ofc)。
我认为这是非常 git 的方式 - 与问题相关的代码在问题分支中,通用代码在通用分支中。
编辑1: 我提议的最终结构:
master
|
Common
/ \
Issue1 Issue2
如果您已经有问题,它可能看起来像这样:
master
/ | \
Issue1 Common1 Issue2
| / | \ /
Merge | Merge
| | |
Issue1 updated Issue2
common
Edit2:另一种解释。
你有主人:
var x = 1;
var y = 2;
print(x+y);
假设您的 Issue1 应该如下所示:
var x = 1;
var y = 3;
printLine(x+y);
function printLine (x) { return print(x + 'line'); }
您的 Issue2 应如下所示:
var x = 1;
var y = 4;
printLine(x+y);
function printLine (x) { return print(x + 'line'); }
然后你创建 Common1 分支:
var x = 1;
var y = 2;
printLine(x+y);
function printLine (x) { return print(x + 'line'); }
因此您的工作流程将如下所示:
git checkout master
git checkout -b common1
//make changes as in Common1
git commit -am common1
git checkout issue1
git merge common1
git checkout issue2
git merge common1
//then you can work on issues. f.e. making changes in issue1
git checkout issue1
//code, code
git commit -am issue1
//if you want to update common methods
git checkout common1
//update
git commit -am updated-common1
git checkout issue1
git merge common1
git checkout issue2
git merge common2