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

相关推荐
火车叼位几秒前
Git 精准移植代码:cherry-pick 简单说明
前端·git
Cynthia-石头3 小时前
Git Github Gitee GitLab
git·gitee·github
ak啊17 小时前
Git 撤销操作完全指南:从工作区到远程仓库的救赎之路
git
小_路20 小时前
git 常用命令
git
Dontla1 天前
git引用概念(git reference,git ref)(简化对复杂SHA-1哈希值的管理)(分支引用、标签引用、HEAD引用、远程引用、特殊引用)
git·算法·哈希算法
枫叶落雨2221 天前
Git 使用规范指南
git
德育处主任2 天前
聚沙成塔,三步成书:GitBook极简入门教程
前端·git·电子书
m0_635647482 天前
git管理github上的repository
git·github
Jiude2 天前
基于开源项目开发公司项目,如何同步开源仓库的最新更新(以vben为例)
git·开源·github
PAK向日葵2 天前
【软件工程】如何使用Git Rebase同步团队开发进度
git·面试