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

标签 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在 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 策略:recursiveresolve 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/

相关文章:

git - 在 msysgit 中运行 MinGW/MSYS 工具

git instaweb 给出 403 Forbidden - 找不到项目

git - 如何使用 git cherry-pick 跳过不精确的重命名检测

git - cherry-pick - HOWTO/WHYTO

git - 如何确定 Git 中的 cherry-pick 的提交者?

git - 如何将 Hudson 构建指向我的本地 git 存储库?

gitkraken - 如何比较 2 个分支

git 切换分支而不丢弃本地更改

git - 为什么 git log --cherry-pick 没有删除等效的提交?

git - 我如何有选择地 merge 或从 Git 中的另一个分支中挑选更改?