在 命令行中 Git cherry-pick 的使用
1、现在需要将分支 A 上的一些提交应用至分支 B 上。
2、先切换至分支:B 上,执行命令:git cherry-pick fbf19e86。报错:CONFLICT (content): Merge conflict in platform/app/Services/ThemeService.php . error: could not apply fbf19e86f4… 。如图1
Auto-merging platform/app/Services/ThemeService.php
CONFLICT (content): Merge conflict in platform/app/Services/ThemeService.php
error: could not apply fbf19e86f4... 安装 2.0 主题模式下的主题。对接 SaaS 中心完成。
hint: After resolving the conflicts, mark them with
hint: "git add/rm ", 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".
PS E:\wwwroot\object> git cherry-pick --abort
3、原因找到,原因是提交 ID fbf19e86 之前还有一些提交,不应该跳过去。如图2
4、执行命令:git cherry-pick –abort,发生代码冲突后,放弃合并,回到操作前的样子。
PS E:\wwwroot\object> git cherry-pick --abort
PS E:\wwwroot\object>
5、执行命令:git cherry-pick 5b0afab6。不再报错。因为这个提交是分支 B 上的我的下一次提交。如图3
PS E:\wwwroot\object> git cherry-pick 5b0afab6
Auto-merging platform/app/Services/ThemeService.php
[feature/theme-store-wangqiang-20220526 531899fd34] 获取 SaaS 主题配置,重新索引多维数组的重构
Date: Tue May 10 15:56:06 2022 +0800
1 file changed, 1 insertion(+), 1 deletion(-)
6、按提交 ID 时间顺序依次执行第 5 步骤,由于一些提交ID是连续提交。如图4
7、可以使用下面的简便语法。存在冲突。解决代码冲突后,第一步将修改的文件重新加入暂存区(git add .),第二步使用下面的命令,让 Cherry pick 过程继续执行。期间使用 vim 编辑器(直接退出),我是在 PhpStorm 中编辑的。如图5
Auto-merging platform/app/Http/Controllers/API/StatisticsController.php
CONFLICT (content): Merge conflict in platform/app/Http/Controllers/API/StatisticsController.php
Auto-merging platform/app/Http/Kernel.php
当前主题中间件替换为:CurrentTheme
# Conflicts:
# platform/app/Http/Controllers/API/StatisticsController.php
#
# It looks like you may be committing a cherry-pick.
# If this is not correct, please run
# git update-ref -d CHERRY_PICK_HEAD
# and try again.
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Tue May 10 17:55:23 2022 +0800
#
# On branch feature/theme-store-wangqiang-20220526
# Your branch is ahead of 'origin/feature/theme-store-wangqiang-20220526' by 1 commit.
# (use "git push" to publish your local commits)
#
# Cherry-pick currently in progress.
#
# Changes to be committed:
# modified: platform/app/Http/Controllers/API/StatisticsController.php
# modified: platform/app/Http/Kernel.php
E:/wwwroot/object/.git/COMMIT_EDITMSG [unix] (18:56 26/05/2022) 2,0-1 Top
[feature/theme-store-wangqiang-20220526 9766f26582] 当前主题中间件替换为:CurrentTheme
Date: Tue May 10 17:55:23 2022 +0800
5 files changed, 109 insertions(+), 166 deletions(-)
delete mode 100644 platform/app/Http/Middleware/ConfigTheme.php
rewrite platform/app/Http/Middleware/CurrentTheme.php (78%)
Auto-merging platform/app/Services/ThemeService.php
[feature/theme-store-wangqiang-20220526 25cf0e2a8a] 安装主题,入库实现
8、查看提交日志,符合预期。选择的提交ID皆已纳入分支 B 中。如图6
9、如果提示:The previous cherry-pick is now empty, possibly due to conflict resolution.。则执行命令:git cherry-pick –skip。
PS E:\wwwroot\object> git cherry-pick --continue
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 feature/theme-store-wangqiang-202205262
Your branch is up to date with 'origin/feature/theme-store-wangqiang-202205262'.
Cherry-pick currently in progress.
(run "git cherry-pick --continue" to continue)
(use "git cherry-pick --skip" to skip this patch)
(use "git cherry-pick --abort" to cancel the cherry-pick operation)
nothing to commit, working tree clean





