从与存储库当前状态不一致的提交中挑选

Cherry-picking from a commit that is inconsistent with the current state of the repository

Cherry-picking 正在将从任意提交复制的一系列差异应用到存储库的当前状态。如果其中一些差异与存储库的当前状态不一致,会发生什么情况?例如,如果其中一个差异修改了文件 f,但该文件不再存在于当前存储库中怎么办?

在这种情况下,就会发生冲突。 Git 暂停 cherry-pick 并等待您解决冲突或中止 cherry-pick。从概念上讲,这与 a conflict that might arise due to a git mergegit rebase.

相同

what if one of the diffs modifies a file f, but this file no longer exists in the current repository

Git 会为您提供选择,是保留您精心挑选的文件、保留删除的文件还是手动更新文件。

很容易亲眼看到它是如何工作的。

  1. 在你的文件系统的某个地方(在现有的 Git 存储库之外),创建一个新目录并进入它。

    mkdir cherry
    cd cherry
    
  2. 初始化一个空的 Git 存储库

    git init
    
  3. 创建文件并向其中添加一些内容

    echo "Some changes" > README.md
    
  4. 将文件添加到索引并提交更改

    git add README.md
    git commit -m "Added some content to the readme"
    
  5. 创建并签出新分支

    git checkout -b feature/readme-update
    
  6. 再次更改README.md文件

    echo "New content of README" > README.md
    
  7. 暂存并提交更改

    git add README.md
    git commit -m "Added more to readme"
    

    保存最后一次提交的哈希值。

  8. 返回master并删除文件,提交更改

    git checkout master
    git rm README.md
    git commit -m "Removed the readme"
    
  9. Cherry-pick 从 feature/readme-update 中更改 README.md 文件内容的提交

    git cherry-pick <commit-hash>
    
  10. 您手上有冲突

    tomek@LAPTOP-SGL6966J MINGW64 /c/repos/cherry (master)
    $ git cherry-pick <commit-hash>
    error: could not apply 4a99ca7... Added more to readme
    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'
    
    tomek@LAPTOP-SGL6966J MINGW64 /c/repos/cherry (master|CHERRY-PICKING)
    

    git status也会告诉你同样的事情

    $ git status
    On branch master
    You are currently cherry-picking commit 4a99ca7.
      (fix conflicts and run "git cherry-pick --continue")
      (use "git cherry-pick --abort" to cancel the cherry-pick operation)
    
    Unmerged paths:
      (use "git add/rm <file>..." as appropriate to mark resolution)
    
        deleted by us:   README.md
    

    在这种情况下,您可以删除文件(接受来自 master 的更改)或添加文件(接受您正在挑选的更改)。另一种选择是以您认为合理的任何方式手动调整文件。

    无论您接受什么更改,将它们应用到文件,暂存它们并通过调用 git cherry-pick --continue

    继续选择

    如果您感到困惑并且不想再继续选择,请调用 git cherry-pick --abort

  11. 中止它