我们在开发中有时会遇到,需要将另一个分支部分修改同步到当前分支。

如下图,想把 devA 分支中 commit E 和 F,同步到下面绿色的 devB 分支中。

这时候就可以使用 git cherry-pick 来完成这项任务。

(cherry-pick 有筛选、精选的意思)

一、基本用法

转移单个提交

git cherry-pick <commitHash>
# 切换到 devB 分支
$ git checkout devB # Cherry pick 操作
$ git cherry-pick <HashE>

解决冲突后,commit 即可

二、转移多个提交

如果我有一堆连续的 commit 都想同步过去,那么可以用下面的语法:

下面的命令可以转移从 E 到 F 的所有 commit。 注意按顺序写:提交 E 必须早于提交 F

git cherry-pick <HashE>..<HashF>

还要注意上面命令是左闭右开的,即不包含 commit_E,如果需要两边都包括,用下面的语法:

git cherry-pick <HashE>^..<HashF>

如果是分开的几个 commit,可以这样写:

git cherry-pick <HashE> <HashG>

三、参数

文档中是这样写的:

usage: git cherry-pick [<options>] <commit-ish>...
or: git cherry-pick <subcommand> --quit end revert or cherry-pick sequence
--continue resume revert or cherry-pick sequence
--abort cancel revert or cherry-pick sequence
--skip skip current commit and continue
--cleanup <mode> how to strip spaces and #comments from message
-n, --no-commit don't automatically commit
-e, --edit edit the commit message
-s, --signoff add a Signed-off-by trailer
-m, --mainline <parent-number>
select mainline parent
--rerere-autoupdate update the index with reused conflict resolution if possible
--strategy <strategy>
merge strategy
-X, --strategy-option <option>
option for merge strategy
-S, --gpg-sign[=<key-id>]
GPG sign commit
-x append commit name
--ff allow fast-forward
--allow-empty preserve initially empty commits
--allow-empty-message
allow commits with empty messages
--keep-redundant-commits
keep redundant, empty commits

提几个会用得到的:

1)-n 如果你想转移多个 commit 并在新分支中只想有一个 commit,那就可以添加 -n 参数,不自动提交代码,都转移后一次性手动提交。(注意如果有 conflict 情况就不是很好用)(为了分辨是从其他分支转移过来的,可以新开一个分支同步这些 commit,然后再 merge 到目标分支)

    -n, --no-commit       don't automatically commit

2)-x 在提交信息的末尾追加一行(cherry picked from commit ...),方便以后查到这个提交是如何产生的。

    -x                    append commit name

3)不建议同步「合并(merge)节点」,得到的结果应该不是你想要的(有兴趣可以自己尝试)。

四、代码冲突

1)--continue

同步代码不可避免遇到冲突情况,解决冲突后,将修改的文件重新加入暂存区 git add .,然后使用下面命令继续:

git cherry-pick --continue

2)--abort

处理过程中可能有误操作,那么可以放弃合并,回到操作前的样子。

git cherry-pick --abort

(3)--quit

发生代码冲突后,退出 cherry pick,但是不回到操作前的样子。

git cherry-pick --quit

最新文章

  1. Android中 Bitmap和Drawable相互转换的方法
  2. 面试时被问到js的绑定事件,我居然不知道怎么回答。回来查了下,做个笔记
  3. 图解AngularJS Wijmo5和LightSwitch
  4. [Whole Web] [Node.js] [Browserify] [Grunt] Automation task with grunt-browserify &amp; grunt-contrib-watch
  5. element.style覆盖了我的样式!!
  6. 配置SSH隧道访问Ubuntu服务器上的MongoDB
  7. DbHelperSQL 增加事务处理方法(2种)
  8. BZOJ2809 dispatching 【可并堆】
  9. 普通socket与netty服务端交互
  10. 截断文件函数truncate和ftruncate
  11. 使用Sense操作ElasticSearch CRUD
  12. MySql(十九):基础——自定义存储过程和函数
  13. Openstack 组件简介
  14. 「Linux」centos7安装mysql
  15. 玩转windowbuilder pro
  16. Unity的shader学习2
  17. C# Winform App 获取当前路径
  18. Core Animation 与 GPU
  19. .NET开源工作流RoadFlow-表单设计-按钮
  20. 两数之和 [ leetcode ]

热门文章

  1. Docker容器获取宿主机信息
  2. SpringBoot+MyBatis Plus对Map中Date格式转换的处理
  3. mysql 过程和函数语法学习笔记
  4. 2022年最新最详细的tomcat安装教程和常见问的解决
  5. 前端框架Vue------>第三天学习(1)
  6. DevOps|从特拉斯辞职风波到研发效能中的不靠谱人干的荒唐事
  7. 部署redis-cluster
  8. 系统整理K8S的配置管理实战-建议收藏系列
  9. onps栈移植说明(3)——添加网卡
  10. Python 学习思路 思维导图 Xmind