考虑由以下命令创建的情况:
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
file
在分支first
包含从 1 到 4 的数字(每个数字在其自己的行中)。同file
在 Twig 上second
file
包含从 1 到 6 的数字。已作为新分支添加到两个分支中。
现在,如果我尝试将一个分支挑选到另一个分支上,我梦想的结果将是(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
您正在使用默认 merge 策略:recursive
。 resolve
merge 策略将产生您可能想要的状态的冲突。
$ 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 cherry-pick 提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38002527/