Git cherry-pick 转移提交

文章目录

  • [1 cherry-pick](#1 cherry-pick)
  • [2 在分支上的基本用法](#2 在分支上的基本用法)
    • [2.1 使用hash值](#2.1 使用hash值)
    • [2.2 使用分支名](#2.2 使用分支名)
  • [3 转移多个提交](#3 转移多个提交)
    • [3.1 转移多个不同的提交](#3.1 转移多个不同的提交)
    • [3.2 转移连续多个提交](#3.2 转移连续多个提交)
  • [4 代码冲突的处理](#4 代码冲突的处理)
    • [4.1 解决冲突](#4.1 解决冲突)
    • [4.2 放弃解决,退回到之前](#4.2 放弃解决,退回到之前)
    • [4.3 cherry-pick is Empty](#4.3 cherry-pick is Empty)
  • 参考

1 cherry-pick

在有多个分支的代码仓上,将一个分支上的代码转移到另一个分支,目的是将个别代码的改动移动到不同的分支

例子:

需要在一个稳定版本上添加一个刚刚开发完的版本中的某个功能,而不是所有的改动,此时就可以使用cherry-pick,将相关的commit提取出来,合入到稳定分支

如果应用分支的全部更改,就是代码合并,使用git merge

2 在分支上的基本用法

2.1 使用hash值

使用方式:切换到需要cherry-pick的分支(即需要获取其他提交的分支),查看需要pick的分支的commitHash,执行cherry-pick后会在最新的提交后增加一个新的提交

shell 复制代码
git switch master
git cherry-pick <CommandHash>

当前分支

shell 复制代码
$ git log --oneline --all  --graph

* 0e2fa05 (HEAD -> br_DEMOV1_test) D
| * a5840ce (br_DEMOV1_dev) C
| * aa3058e B
|/
* 1d68f85 (master) A

br_DEMOV1_dev中的提交B,cherry-pick到br_DEMOV1_test分支后

shell 复制代码
$ git cherry-pick aa3058

$ git log --oneline --all --graph

* 603d6c9 (HEAD -> br_DEMOV1_test) B
* 0e2fa05 D
| * a5840ce (br_DEMOV1_dev) C
| * aa3058e B
|/
* 1d68f85 (master) A

2.2 使用分支名

直接使用分支名也是可以的,采用的是某个分支最新的提交

shell 复制代码
git cherry-pick my_branch
shell 复制代码
$ git log --oneline --all  --graph

* 0e2fa05 (HEAD -> br_DEMOV1_test) D
| * a5840ce (br_DEMOV1_dev) C
| * aa3058e B
|/
* 1d68f85 (master) A

直接采用C分支

shell 复制代码
$ git cherry-pick br_DEMOV1_dev

$ git log --oneline --all  --graph

* 29dcd60 (HEAD -> br_DEMOV1_test) C
* 0e2fa05 D
| * a5840ce (br_DEMOV1_dev) C
| * aa3058e B
|/
* 1d68f85 (master) A

3 转移多个提交

3.1 转移多个不同的提交

直接填多个不同提交的hash即可

shell 复制代码
* 910f8e5 (HEAD -> br_DEMOV1_dev) F
* a5840ce C
* aa3058e B
| * e7fac42 (br_DEMOV1_test) E
| * 0e2fa05 D
|/
* 1d68f85 (master) A

将B和F应用到br_DEMOV1_test分支

shell 复制代码
$ git cherry-pick 910f a584

$ git log --oneline --all  --graph

* 84e98f4 (HEAD -> br_DEMOV1_test) C
* 8b1c88c F
* e7fac42 E
* 0e2fa05 D
| * 910f8e5 (br_DEMOV1_dev) F
| * a5840ce C
| * aa3058e B
|/
* 1d68f85 (master) A

可以看C放到了最新的提交

3.2 转移连续多个提交

shell 复制代码
* 910f8e5 (HEAD -> br_DEMOV1_dev) F
* a5840ce C
* aa3058e B
| * e7fac42 (br_DEMOV1_test) E
| * 0e2fa05 D
|/
* 1d68f85 (master) A

转移连续多个提交,使用.., 转移B->F到分支br_DEMOV1_test

shell 复制代码
$ git cherry-pick 1d68..910f

$ git log --oneline --all  --graph

* 763f180 (HEAD -> br_DEMOV1_test) F
* f0a9b2a C
* 41a99fa B
* e7fac42 E
* 0e2fa05 D
| * 910f8e5 (br_DEMOV1_dev) F
| * a5840ce C
| * aa3058e B
|/
* 1d68f85 (master) A

注意 1d68 不会包含在内

4 代码冲突的处理

4.1 解决冲突

shell 复制代码
$ git log --oneline --all  --graph
* 9c66dae (HEAD -> br_DEMOV1_test) F
* f0a9b2a C
* 41a99fa B
* e7fac42 E
* 0e2fa05 D
| * 910f8e5 (br_DEMOV1_dev) F
| * a5840ce C
| * aa3058e B
|/
* 1d68f85 (master) A

均对F进行修改,此时发生冲突,进入CHERRY-PICKING

shell 复制代码
$ git cherry-pick 9c66
Auto-merging F.py
CONFLICT (add/add): Merge conflict in F.py
error: could not apply 9c66dae... F
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config advice.mergeConflict false"

修改文件后,保存变更,而后继续 cherry-pick

shell 复制代码
git add [file]
git cherry-pick --continue

4.2 放弃解决,退回到之前

进入CHERRY-PICKING执行--abort

shell 复制代码
git cherry-pick --abort

4.3 cherry-pick is Empty

使用cherry-pick的时候报错

shell 复制代码
The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyway, use:

    git commit --allow-empty

Otherwise, please use 'git cherry-pick --skip'
On branch br_DEMOV1_dev
You are currently cherry-picking commit 9c66dae.
  (all conflicts fixed: run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

即cherry-pick的提交相对于当前没有任何的更改,此时可能是在当前的分支cherry-pick当前,此时推出即可;对于没有变更分支的cherry-pick,需要根据需求进行选择


参考

git cherry-pick教程 https://www.ruanyifeng.com/blog/2020/04/git-cherry-pick.html

相关推荐
SiYuanFeng40 分钟前
新手学Git:如何把本地 Git 项目上传到 GitHub
git·github
前端若水2 小时前
git回退并合并分支操作
git
程序员鱼皮8 小时前
Git WorkTree 是什么?凭什么能让 AI 编程效率翻倍?
git·ai·程序员·编程·ai编程
懵逼的小黑子10 小时前
git与远程仓库创建连接
git
前端若水10 小时前
Git 撤销与恢复完全指南(超级详细版)
大数据·git·elasticsearch
golang学习记10 小时前
Git 2.54 来了,这个新命令让我终于敢重写历史了
git·后端
其实防守也摸鱼10 小时前
AWVS下载和安装保姆级教程
linux·服务器·git
前端若水11 小时前
Git 可以做的所有操作(完整分类)
大数据·git·elasticsearch
叹一曲当时只道是寻常11 小时前
Reference 工具安装与使用教程:一条命令管理 Git 仓库引用与知识沉淀
人工智能·git·ai·开源·github
前端若水12 小时前
Git 仓库管理命令完全指南(超级详细版)
大数据·git·elasticsearch