Git 添加相同文件的 cherry-pick 提交

Git cherry-pick commit that adds the same file

考虑以下命令造成的情况:

git init
git commit --allow-empty -m "Initial commit"
git branch first
git branch second
git checkout first
echo 1 > file
echo 2 >> file
echo 3 >> file
echo 4 >> file
git add file
git commit -m "Commit file 1 to 4"
git checkout second
echo 1 > file
echo 2 >> file
echo 3 >> file
echo 4 >> file
echo 5 >> file
echo 6 >> file
git add file
git commit -m "Commit file 1 to 6"
git checkout first
git cherry-pick second

分支 first 上的 file 包含从 1 到 4 的数字(各占一行)。分支 second 上的相同 file 包含从 1 到 6 的数字。file 已作为新分支添加到两个分支中。

现在,如果我尝试将一个分支挑选到另一个分支上,我梦想的结果将是(file 内容):

1
2
3
4
5
6

可接受的结果是

1
2
3
4
<<<<<<< HEAD
=======
5
6
>>>>>>> 5c9d53e... Commit file 1 to 6

然而,git总是给我:

<<<<<<< HEAD
1
2
3
4
=======
1
2
3
4
5
6
>>>>>>> 5c9d53e... Commit file 1 to 6

而且我必须自己解决所有冲突。

如何挑选两个相互添加相同文件(可能具有相似内容)的提交?如何使 git 仅在需要时才尝试分析其内容并使 运行 发生冲突?

现在它的行为就像 嘿!这些提交添加相同的文件,所以我将在这里抛出整个文件冲突!我懒得看里面了

git init
git commit --allow-empty -m "Initial commit"
git branch first
git branch second
git checkout first
echo 1 > file
echo 2 >> file
echo 3 >> file
echo 4 >> file
git add file
git commit -m "Commit file 1 to 4"
git checkout second
echo 1 > file
echo 2 >> file
echo 3 >> file
echo 4 >> file
echo 5 >> file
echo 6 >> file
git add file
git commit -m "Commit file 1 to 6"
git checkout first

#Here is where I did edits:

git cherry-pick --strategy resolve second
git diff HEAD..second
git add file
git commit -C second

您正在使用默认合并策略:recursiveresolve 合并策略将产生您可能想要的状态的冲突。

$ git diff HEAD..second
diff --git a/file b/file
index 94ebaf9..b414108 100644
--- a/file
+++ b/file
@@ -2,3 +2,5 @@
 2
 3
 4
+5
+6

$ cat file
1
2
3
4
5
6

挑选之后需要运行

git checkout --conflict=merge file

为了获得file可接受的内容,即:

1
2
3
4
<<<<<<< ours
=======
5
6
>>>>>>> theirs

--strategy resolve 和 Git 2.9 都没有解决我的问题,正如@BryceDrew 和@torek 分别建议的那样。