首页·终端命令

git cherry-pick

git

用法

git cherry-pick [-x] <commit>...

把某一个提交以新的哈希复制到当前分支;之后再合并那个分支时,同一处改动可能出现两次。

常用选项

选项含义
-xnote the original hash in the new message
-napply and stage without committing
-eedit the message
-m <parent>pick a merge commit, choosing a side
<a>..<b>a range; a itself is not included
--continuego on after a conflict
--abortundo the whole pick

示例

git cherry-pick a1b2c3d

copies that commit onto the current branch with a new hash

git cherry-pick -x a1b2c3d

same, with the source hash recorded in the message

git cherry-pick a1b2c3d^..d4e5f6a

copies a range, this time including a1b2c3d

难的地方在于撤销有好几种:把分支的指向挪走,和补一个反向提交,不是一回事。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. git cherry-pick 是做什么的?

把某一个提交以新的哈希复制到当前分支;之后再合并那个分支时,同一处改动可能出现两次。

Q. 怎么写?

git cherry-pick [-x] <commit>... —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 7 个,完整列表在 man git。这条命令属于git。

相关命令

man 手册: man git-cherry-pick