首页 > 解决方案 > git merge-file 将整个文件标记为不同而不是部分

问题描述

我正在尝试在很久以前逐个文件分歧的git merge-file两个分支(current和)之间进行 3 路合并。other我之所以选择这种方法,是因为这样做git merge会导致太多的冲突并使其成为一场噩梦,而在一个文件基础上进行操作不会破坏所有内容,并且随着更改的进行更新所有测试会更容易。

然后的想法是使用这个脚本,它接受一个分支名称、一个文件名,并使用当前分支上文件的最后一个版本、分歧点的版本和另一个分支上最后一次提交的版本执行合并.

问题是,这个命令不是只突出显示不同的部分,而是简单地说整个文件有分歧,即使实际上只有几行不同。我在这里做错了什么?

#!/bin/bash

common_sha=$(git merge-base HEAD $1)

git show $common_sha:$2 > common
git show $1:$2 > theirs

git merge-file --diff3 $2 common theirs

rm common
rm theirs

这是正在使用的脚本的示例:

$ git branch -a
* current_branch
  master
  other_branch
$ my_merge.sh other_branch file.txt

这是它使用的文件

common version of file.txt

line 1
line 2
line 3
line 4
line 5

current branch's version of file.txt

line 1
line 2.2
line 3
line 4.1
line 6

other branch's version of file.txt

line 1
line 2.1
line 2.2
line 3
line 4
line 5

以及预期与实际结果

expected result after running the script

line 1
<<<<<<< file.txt
line 2.2
||||||| common
line 2
=======
line 2.1
line 2.2
>>>>>>> theirs
line 3
line 4.1
line 6

actual result

<<<<<<< file.txt
line 1
line 2.2
line 3
line 4.1
line 6
||||||| common
line 1
line 2
line 3
line 4
line 5
=======
line 1
line 2.1
line 2.2
line 3
line 4
line 5
>>>>>>> theirs


标签: git

解决方案


检查文件:

git show HEAD:a-file > a.txt
git show $1:a-file > b.txt # replace $1 for the revision you are playing with
git show $( git merge-base HEAD $1 ):a-file > c.txt

在它们file a.txt b.txt c.txt(可以告诉你格式(unix/windows)。如果它们混合在一起,这就是您看到该问题的原因。


推荐阅读