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

时间:2016-06-23 21:54:41

标签: git git-cherry-pick

考虑以下命令创建的情况:

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上的second包含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尝试分析其内容并在需要时遇到冲突?

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

在樱桃挑选之后需要运行

git checkout --conflict=merge file

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

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

@ --strategy resolve和Git 2.9都没有为我解决问题,分别由@BryceDrew和@torek建议。