cherry-picking commit - 提交快照或补丁?

MrC*_*ket 2 git cherry-pick

我有一个与挑选提交和冲突有关的问题.

'Pro Git'一书解释了提交是一种快照,而不是补丁/差异.

但挑选樱桃可能会表现得像补丁一样.


以下示例,简而言之:

  1. 创建3个提交,每次编辑文件的第一行(和单行)

  2. 将分支重置为首次提交

  3. test1:尝试樱桃挑选第三次提交(冲突)

  4. 测试2:尝试樱桃挑选第二次提交(OK)


mkdir gitlearn
cd gitlearn

touch file
git init
Initialized empty Git repository in /root/gitlearn/.git/

git add file

#fill file by single 'A'
echo A > file && cat file
A

git commit file -m A
[master (root-commit) 9d5dd4d] A
 1 file changed, 1 insertion(+)
 create mode 100644 file

#fill file by single 'B'
echo B > file && cat file
B

git commit file -m B
[master 28ad28f] B
 1 file changed, 1 insertion(+), 1 deletion(-)

#fill file by single 'C'
echo C > file && cat file
C

git commit file -m C
[master c90c5c8] C
 1 file changed, 1 insertion(+), 1 deletion(-)

git log --oneline
c90c5c8 C
28ad28f B
9d5dd4d A
Run Code Online (Sandbox Code Playgroud)

测试1

#reset the branch to 9d5dd4d ('A' version)
git reset --hard HEAD~2
HEAD is now at 9d5dd4d A

git log --oneline
9d5dd4d A

#cherry-pick 'C' version over 'A'
git cherry-pick c90c5c8
error: could not apply c90c5c8... C
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

#the conflict:
cat file
<<<<<<< HEAD
A
=======
C
>>>>>>> c90c5c8... C
Run Code Online (Sandbox Code Playgroud)

测试2

#same for 'B' - succeeds
git reset --hard HEAD
HEAD is now at 9d5dd4d A

git cherry-pick 28ad28f
[master eb27a49] B
 1 file changed, 1 insertion(+), 1 deletion(-)
Run Code Online (Sandbox Code Playgroud)

请解释为什么测试1失败(如果提交是补丁,我可以想象答案,但快照?)

tor*_*rek 7

Pro Git书是正确的:提交是快照.

不过,您也是正确的:git cherry-pick应用补丁.

怎么会这样?答案是,当您挑选提交时,您还可以使用参数指定要考虑的提交.cherry-pick命令然后生成针对该父级的diff,以便现在可以应用生成的diff.-m parent-number

如果你选择樱桃选择非合并提交,那么只有一个父级,所以你实际上没有传递-m,命令使用(单个)父级来生成diff.但是提交本身仍然是一个快照,它cherry-pick是找到commit^1(第一个也是唯一的一个)vs 的差异的命令commit并应用它.