简体   繁体   English

添加相同文件的Git Cherry-Pick提交

[英]Git cherry-pick commit that adds the same file

Consider the situation created by the following commands: 考虑以下命令创建的情况:

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

The file on branch first contains numbers from 1 to 4 (each in its own line). 分支上的file first包含从1到4的数字(每个数字都在其自己的行中)。 The same file on the branch second contains numbers from 1 to 6. The file has been added in both branches as a new one. 分支second上的同一file包含从1到6的数字。该file已在两个分支中作为新file添加。

Now, if I try to cherry-pick one branch onto another, my dreamed result would be (the file contents): 现在,如果我尝试将一个分支挑选到另一个分支,我的梦想结果将是( file内容):

1
2
3
4
5
6

An acceptable result would be 可接受的结果是

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

However, git always gives me: 但是,git总是给我:

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

And I have to do all of the conflict resolution myself. 我必须自己解决所有冲突。

How to cherry-pick two commits that add the same file (with possibly similar content) on each other? 如何挑选两个相互提交相同文件(内容可能相似)的提交? How to make git try to analyze their contents and run into conflict only if it needs to? 如何使git仅在需要时尝试分析其内容并发生冲突?

Now it behaves like Hey! 现在,它的行为就像嘿! These commits add the same file so I will throw a whole file conflict here! 这些提交会添加相同的文件,因此我将在此处引发整个文件冲突! I'm too lazy to look inside them. 我懒得看不到他们的内心。

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

You are using the default merge strategy: recursive . 您正在使用默认的合并策略: recursive The resolve merge strategy will produce the conflict in the state you might want. resolve合并策略将在您可能想要的状态下产生冲突。

$ 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

After the cherry-pick one needs to run 摘樱桃后需要跑

git checkout --conflict=merge file

in order to get the acceptable content of the file , ie: 为了获得file可接受内容,即:

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

Neither --strategy resolve nor the Git 2.9 solved the problem for me, as suggested by @BryceDrew and @torek respectively. 无论--strategy resolve ,也不是Git的2.9解决了这个问题对我来说,由@BryceDrew和@torek分别建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM