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

时间:2015-01-03 22:33:13

标签: git cherry-pick

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

' Pro Git'提交的书explains是一种快照,而不是补丁/差异。

但挑选樱桃的行为可能与补丁一样。


以下示例,简而言之:

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

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

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

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


  5. 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
    

    测试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
    

    测试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(-)
    

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

1 个答案:

答案 0 :(得分:7)

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

你也是对的:git cherry-pick应用补丁。

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

如果您选择非合并提交,则只有一个父级,因此您实际上并未传递-m,并且该命令使用(单个)父级来生成差异。但是提交本身仍然是一个快照,它是cherry-pick命令,用于查找commit^1(第一个也是唯一的父级)vs commit 的差异并应用这一点。

相关问题