git中cherry-pick命令的使用教程

作者:小旭2021 时间:2023-02-11 00:33:57 

git cherry-pick可以选择某一个分支中的一个或几个commit(s)来进行操作。例如,假设我们有个稳定版本的分支,叫v2.0,另外还有个开发版本的分支v3.0,我们不能直接把两个分支合并,这样会导致稳定版本混乱,但是又想增加一个v3.0中的功能到v2.0中,这里就可以使用cherry-pick了,其实也就是对已经存在的commit 进行再次提交。

简单用法:

git cherry-pick <commit id>

例如:

$ git checkout v2.0分支
$ git cherry-pick 38361a55 # 这个 38361a55 号码,位于v3.0分支中:

$ git log
commit 38361a55138140827b31b72f8bbfd88b3705d77a
Author: Justin Justin@xxx.com
Date: Sat Dec 10 00:11:44 2016 +0800

1. 如果顺利,就会正常提交。结果:

Finished one cherry-pick.
On branch v2.0分支
Your branch is ahead of 'origin/old_cc' by 3 commits.

2. 如果在cherry-pick 的过程中出现了冲突

Automatic cherry-pick failed.
After resolving the conflicts,mark the corrected paths with 'git add <paths>' or 'git rm <paths>'and commit the result with:
git commit -c 15a2b6c61927e5aed6111de89ad9dafba939a90b

或者:

error: could not apply 0549563... dev
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'

就跟普通的冲突一样,手工解决:

2.1 $ git status # 看哪些文件出现冲突

both modified: app/models/MainActivity.java

2.2 $ vim app/models/MainActivity.java # 手动解决它。

2.3 $ git add app/models/MainActivity.java

2.4 git commit -c <新的commit号码>

2.5 再次cherry-pick剩余commit

若提示:

error: a cherry-pick or revert is already in progress
hint: try "git cherry-pick (--continue | --quit | --abort)"
fatal: cherry-pick failed

则执行对应操作:

git cherry-pick --continue
git cherry-pick --quit
git cherry-pick --abort

命令集合:

git cherry-pick <commit id>:单独合并一个提交
git cherry-pick -x <commit id>:同上,不同点:保留原提交者信息。
Git从1.7.2版本开始支持批量cherry-pick,就是一次可以cherry-pick一个区间的commit。
git cherry-pick <start-commit-id>..<end-commit-id>
git cherry-pick <start-commit-id>^..<end-commit-id>

前者表示把<start-commit-id>到<end-commit-id>之间(左开右闭,不包含start-commit-id)的提交cherry-pick到当前分支;
后者有"^"标志的表示把<start-commit-id>到<end-commit-id>之间(闭区间,包含start-commit-id)的提交cherry-pick到当前分支。
其中,<start-commit-id>到<end-commit-id>只需要commit-id的前6位即可,并且<start-commit-id>在时间上必须早于<end-commit-id>
注:以上合并,需要手动push代码。

以上内容总结如下:

1.git checkout master            //此操作需要切换到master分支  

2.git pull

3.git cherry-pick  xxxxxx

4.git status

5.编译提交等

来源:https://www.cnblogs.com/chenyablog/p/8484556.html

标签:git,cherry-pick,命令
0
投稿

猜你喜欢

  • Go http请求排队处理实战示例

    2024-05-21 10:26:58
  • python神经网络学习使用Keras进行简单分类

    2023-09-18 04:37:23
  • python通过伪装头部数据抵抗反爬虫的实例

    2022-03-11 09:56:04
  • Python编程tkinter库Canvas实现涂鸦颜色表及围棋盘示例

    2023-08-02 13:21:02
  • DOM_window对象属性之--clipboardData对象操作代码

    2011-02-05 10:49:00
  • vue混入mixin流程与优缺点详解

    2024-05-02 16:35:12
  • Linux下MySQL多实例部署及安装指南

    2024-01-21 10:49:28
  • 用Python判断奇偶数示例

    2021-03-17 22:18:22
  • 如何在Django项目中引入静态文件

    2021-10-09 11:24:52
  • 六个窍门助你提高Python运行效率

    2021-07-19 08:31:15
  • 数据库基础:MySQL 添加用户的两种方法

    2009-05-07 14:26:00
  • python绘制带有色块的折线图

    2022-08-11 07:01:58
  • Perl集群配置管理系统Rex简明手册

    2023-07-15 20:00:34
  • 利用python为运维人员写一个监控脚本

    2021-01-19 16:12:37
  • Python 3.x基础实战检查磁盘可用空间

    2021-05-11 09:21:25
  • python中列表添加元素的几种方式(+、append()、extend())

    2022-07-17 19:59:42
  • Python实现将一个正整数分解质因数的方法分析

    2021-01-09 10:39:29
  • Python通过pymysql调用MySQL进行增删改移查

    2024-01-28 07:59:17
  • 微信小程序搭建及解决登录失败问题

    2023-06-28 09:51:49
  • Python3中对range()逆序的解释

    2023-03-26 10:27:59
  • asp之家 网络编程 m.aspxhome.com