28

我的存储库中有一些文件在底部增长:大多数更改涉及在文件底部添加新行。这主要是语言和其他属性文件。

作为一个恼人的副作用,每当两个人同时进行添加时,我都会遇到合并冲突,并且解决方法总是涉及手动复制粘贴,以便包含来自两个版本的行。

有什么技巧、窍门或方法可以减轻这个过程的一些痛苦吗?

例如,一个简单的解决方案是告诉开发人员在文件中间的随机位置添加新行。这可能会奏效,但它需要有意识的努力,以及一段看起来很奇怪的历史。

4

3 回答 3

17

您可以使用gitattributes机制来定义自定义合并驱动程序(例如这个),以便自动复制相关部分。

[merge "aggregate"]
        name = agregate both new sections
        driver = aggregate.sh %O %A %B

这将是一个 3 路合并,这意味着您可以轻松地比较%A%B反对%O(共同祖先)以隔离所述新部分,并将它们聚合到结果合并文件中。

该聚合合并驱动程序只需要执行以下操作:

comm -13 $1 $3 >> $2

(comm 实用程序是 GoW 的一部分——Windows 上的Gnu——发行版,如果你在 Windows 上)


这是一个小演示:

首先,让我们建立一个 Git 存储库,在两个分支(' master' 和 ' abranch')中修改一个文件:

C:\prog\git\tests>mkdir agg
C:\prog\git\tests>cd agg
C:\prog\git\tests\agg>git init r1
Initialized empty Git repository in C:/prog/git/tests/agg/r1/.git/
C:\prog\git\tests\agg>cd r1

# Who am I?
C:\prog\git\tests\agg\r1>git config user.name VonC
C:\prog\git\tests\agg\r1>git config user.email vonc@xxx

# one file, first commit:
C:\prog\git\tests\agg\r1>echo test > test.txt
C:\prog\git\tests\agg\r1>git add .
C:\prog\git\tests\agg\r1>git commit -m "first commit"
[master c34668d] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

# Let's add one more common line:
C:\prog\git\tests\agg\r1>echo base >> test.txt
C:\prog\git\tests\agg\r1>more test.txt
test
base
C:\prog\git\tests\agg\r1>git add .
C:\prog\git\tests\agg\r1>git commit -m "base"
[master d1cde8d] base
 1 file changed, 1 insertion(+)

现在我们创建一个新分支,并在该文件的两个版本中进行并发修改,就像问题中指定的OP itsadok一样。

C:\prog\git\tests\agg\r1>git checkout -b abranch
Switched to a new branch 'abranch'
C:\prog\git\tests\agg\r1>echo "modif from abranch" >> test.txt
C:\prog\git\tests\agg\r1>git add .
C:\prog\git\tests\agg\r1>git commit -m "abranch contrib"
[abranch a4d2632] abranch contrib
 1 file changed, 1 insertion(+)
C:\prog\git\tests\agg\r1>type test.txt
test
base
"modif from abranch"

# back to master
C:\prog\git\tests\agg\r1>git checkout master
Switched to branch 'master'
C:\prog\git\tests\agg\r1>echo "contrib from master" >> test.txt
C:\prog\git\tests\agg\r1>git add .
C:\prog\git\tests\agg\r1>git commit -m "contrib from master"
[master 45bec4d] contrib from master
 1 file changed, 1 insertion(+)
C:\prog\git\tests\agg\r1>type test.txt
test
base
"contrib from master"

我们有两个分支(注意:git lg是我的别名

C:\prog\git\tests\agg\r1>git lg
* 45bec4d - (HEAD, master) contrib from master (86 minutes ago) VonC
| * a4d2632 - (abranch) abranch contrib (86 minutes ago) VonC
|/
* d1cde8d - base (87 minutes ago) VonC
* c34668d - first commit (89 minutes ago) VonC

现在让我们尝试合并:

C:\prog\git\tests\agg\r1>git merge abranch
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.

C:\prog\git\tests\agg\r1>more test.txt
test
base
<<<<<<< HEAD
"contrib from master"
=======
"modif from abranch"
>>>>>>> abranch

... 广告失败 ;) Agit merge --abort将重置情况。

让我们放置我们的合并驱动程序

C:\prog\git\tests\agg\r1>git config merge.aggregate.name "aggregate both new sections"
C:\prog\git\tests\agg\r1>git config merge.aggregate.driver "aggregate.sh %O %A %B"
C:\prog\git\tests\agg\r1>echo test.txt merge=aggregate > .gitattributes

此时,合并仍然失败:

C:\prog\git\tests\agg\r1>git merge abranch
aggregate.sh .merge_file_a09308 .merge_file_b09308 .merge_file_c09308: aggregate.sh: command not found
fatal: Failed to execute internal merge

正常:我们需要编写该脚本,并将其添加到PATH

vim aggregate.sh:
#!/bin/bash

# echo O: $1
# echo A: $2
# echo B: $3

# After http://serverfault.com/q/68684/783
# How can I get diff to show only added and deleted lines?
# On Windows, install GoW (https://github.com/bmatzelle/gow/wiki/)
ob=$(comm -13 $1 $3)
# echo "ob: ${ob}"

echo ${ob} >> $2

----

C:\prog\git\tests\agg\r1>set PATH=%PATH%;C:\prog\git\tests\agg\r1

现在,合并aggregate驱动程序可以运行

C:\prog\git\tests\agg\r1>git merge --no-commit abranch
Auto-merging test.txt
Automatic merge went well; stopped before committing as requested

C:\prog\git\tests\agg\r1>type test.txt
test
base
"contrib from master"
"modif from abranch"

给你:test.txt文件的末尾abranch已添加到master.

于 2012-08-07T07:49:19.730 回答
6

另一种解决方案是使用--union管道命令选项git-merge-file

[merge "aggregate"]
    name = aggregate both new sections
    driver = git merge-file --union -L %P %A %O %B

这比使用comm:更可靠comm,当输入行没有按照 排序时,可能会生成错误的输出LC_COLLATE

于 2015-12-29T18:48:38.757 回答
1

如果您不想重新添加已删除的行,则必须使用稍微复杂一点的咒语comm

tmp=$(mktemp)
(comm -12 %A %B ; comm -13 %O %A ; comm -13 %O %B ) >| $tmp
mv $tmp %A

首先,您选择本地和远程修订共同的所有行;这可以确保所有已删除的行都保持删除状态。然后添加远程修订添加的行,然后添加本地修订添加的行。

这可以是可以.gitconfig使用单个命令安装的策略:

git config merge.aggregate.driver 'tmp=$(mktemp) ; (comm -12 %A %B ; comm -13 %O %A ; comm -13 %O %B ) >| $tmp ; mv $tmp %A'
于 2014-07-11T14:31:36.190 回答