当我 merge 两个 PHP 类时,有时会出现一个反复出现的问题,其中两个分支都向此类添加了一个函数。假设我有这种情况:
分支A
class MyClass {
/* ... previous content ... */
public function newFunctionA() {
/* ... Content for function A ... */
}
}
分支B
class MyClass {
/* ... previous content ... */
public function newFunctionB() {
/* ... Content for function B ... */
}
}
当我将 Branch B merge 到 Branch A 时,我得到以下信息:
class MyClass {
/* ... previous content ... */
<<<<<<< HEAD
public function newFunctionA() {
/* ... Content for function A ... */
=======
public function newFunctionB() {
/* ... Content for function B ... */
}
>>>>>>> branchB
}
它包括两个函数,但出于某种原因,它省略了第一个函数的右大括号 (}
)。
我知道它很容易解决,但接受这两个内容并自己插入大括号,但我想知道我的编码风格有什么可以改变的,以促进 git 的 merge 过程并防止这些冲突吗?
最佳答案
答案可能不是,但也可能是
我们无法确定,因为您没有向我们展示某些内容(Git 也没有向您展示)。具体来说,您在这里缺少第三个输入,即merge base 提交。
如果我们能看到实际的冲突,我们可以肯定地说更多。要查看实际冲突,如果将 merge.conflictStyle
设置为 diff3
,我们需要查看文件中显示的内容。
发生了什么
当 Git merge 工作时,它不仅仅查看两个分支尖端提交。也就是说,它不是比较:
class MyClass {
/* ... previous content ... */
public function newFunctionA() {
/* ... Content for function A ... */
}
}
对比
class MyClass {
/* ... previous content ... */
public function newFunctionB() {
/* ... Content for function B ... */
}
}
相反,它产生了两个差异。一比较:
class MyClass {
/* ... previous content ... */
}
到:
class MyClass {
/* ... previous content ... */
public function newFunctionA() {
/* ... Content for function A ... */
}
}
这两者之间的区别是,例如,形式的说明:
- 在第42行,添加一个空行
- 在第 43 行,添加
public function newFunctionA() {
- 在第44行,添加一个空行
- 在第 45 行,添加 ...
- 在第 52 行,添加
由于 Git 的差异格式,所有“添加行” block 都聚集在一起,但是两个输入文件中的任何匹配行(例如空行)都可能导致第二“添加一些东西” block 。
然后,Git 分别比较:
class MyClass {
/* ... previous content ... */
}
(这是第一个比较中左侧的相同版本的文件)到:
class MyClass {
/* ... previous content ... */
public function newFunctionB() {
/* ... Content for function B ... */
}
}
这也会生成一系列更改说明。任何在左侧(原始)与右侧(新内容)匹配的行都可以被视为“相同”。
然后 Git 尝试组合这两组指令。
我们需要看到的是两组指令,而不是冲突本身。特别是,如果指令非常相似——就像它们在你的情况下一样——默认情况下,Git 会继续并 merge 它可以的部分,并且只在它可以的部分周围留下冲突标记。吨。所以我们不知道冲突真正开始和结束的地方。
当使用 diff3
作为冲突样式时,Git 会保留完全冲突,显示介于两者之间的 merge 基础版本。
更多信息和一些示例
Git 2.11 为 git diff
添加了一个新的启发式。 (在我看来,git merge
从未在内部使用过这种启发式方法;如果是,我认为您更有可能得到您想要的东西。但我对此无法确定。)参见 this answer from VonC .有关详细信息,请参阅 commit 433860f3d0beb0c6f205290bd16cda413148f098
在 Git 源代码中。
TL;DR 总结是启发式改变了 git diff
的输出:
--- a/9c572b21dd090a1e5c5bb397053bf8043ffe7fb4:git-send-email.perl
+++ b/6dcfa306f2b67b733a7eb2d7ded1bc9987809edb:git-send-email.perl
@@ -231,6 +231,9 @@ if (!defined $initial_reply_to && $prompting) {
}
if (!$smtp_server) {
+ $smtp_server = $repo->config('sendemail.smtpserver');
+}
+if (!$smtp_server) {
foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
if (-x $_) {
$smtp_server = $_;
(注意差异如何在“错误的”行集上同步)到:
--- a/9c572b21dd090a1e5c5bb397053bf8043ffe7fb4:git-send-email.perl
+++ b/6dcfa306f2b67b733a7eb2d7ded1bc9987809edb:git-send-email.perl
@@ -230,6 +230,9 @@ if (!defined $initial_reply_to && $prompting) {
$initial_reply_to =~ s/(^\s+|\s+$)//g;
}
+if (!$smtp_server) {
+ $smtp_server = $repo->config('sendemail.smtpserver');
+}
if (!$smtp_server) {
foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
if (-x $_) {
(请注意它现在如何位于“正确”的一组线上,对人眼而言)。这两个差异在对文件所做的更改方面是等价的,但我们更喜欢第二个。此外,如果 git merge
正在 merge 更改,我们更喜欢 git merge
查看第二个.
请注意,这只是一种启发式方法,因此它是否适用于您的特定更改对是另一个问题。自己运行两个 git diff
操作(开启启发式,因为它在现代 Git 中是默认设置的,并使用 --no-indent-heuristic
关闭它)会告诉你是否如此。
关于php - Git 在 merge 分支时混合两个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72706156/