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

相关推荐
jingshaoqi_ccc6 小时前
GitKraken最后一个免费版本和下载地址
git·github·gitkraken·版本管理工具
乌云暮年6 小时前
Git简单命令
git·gitee·github·batch命令
用户1259265423209 小时前
使用 Docker 搭建 Gitea 并实现 Git HTTP 自动登录
git
一只毛驴12 小时前
谈谈对git stash的理解?
git
长风破浪会有时呀17 小时前
Git 学习笔记
笔记·git·学习
中微子1 天前
Git Rebase 详解:概念、原理与实战示例
git
荔枝吻1 天前
【保姆级喂饭教程】Windows下安装Git Flow
windows·git·git flow
云和数据.ChenGuang1 天前
git中的指令解释
git
小Lu的开源日常1 天前
在 macOS 上设置 SSH 和 Git
git·macos·ssh
eleven_h1 天前
ERROR: Permission to Splode/pomotroid.git deni
git