在旧提交中恢复文件的一部分

Recover part of a file in an old commit

场景是这样的。

commit -am "1 create book.txt
commit -am "2 add content to book.txt"
commit -am "3 delete content from book.txt and replace it new content"

现在我们想恢复在提交 2 中添加并在提交 3 中删除的内容。我们尝试将提交 2 与提交 3 合并,但这不起作用。当然,我们可以只签出提交 2,复制我们删除的内容,然后在签出提交 3 后再次粘贴。有没有办法做到这一点,但是,使用类似于合并、变基或修补的方法?最终结果应该 我们在提交 2 中添加的内容 我们在提交 3 中添加的内容。

这正是 cherry-pick 的用途。

使用 git cherry-pick commit-2 在当前状态之上重播该提交。