没有不值得去解决的问题,也没有不值得去学习的技术!

How to Use Git Cherry-Pick in the Command Line

I run the command: git cherry-pick 5b0afab6. No error is reported, because this commit is my next commit on branch B

1. Now I need to apply some commits from branch A to branch B.

2. First, I switch to branch B and run the command: git cherry-pick fbf19e86. It reports an error: CONFLICT (content): Merge conflict in platform/app/Services/ThemeService.php . error: could not apply fbf19e86f4… . As shown in Figure 1

First, I switch to branch B and run the command: git cherry-pick fbf19e86. It reports an error: CONFLICT (content): Merge conflict in platform/app/Services/ThemeService.php . error: could not apply fbf19e86f4... .
Figure 1
Plaintext
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. I found the cause: there were other commits before commit ID fbf19e86 that should not have been skipped. As shown in Figure 2

I found the cause: there were other commits before commit ID fbf19e86 that should not have been skipped
Figure 2

4. I run the command: git cherry-pick –abort. After a code conflict occurs, this aborts the merge and reverts to the state before the operation.

Plaintext
PS E:\wwwroot\object> git cherry-pick --abort
PS E:\wwwroot\object>

5. I run the command: git cherry-pick 5b0afab6. No error is reported, because this commit is my next commit on branch B. As shown in Figure 3

I run the command: git cherry-pick 5b0afab6. No error is reported, because this commit is my next commit on branch B
Figure 3
Plaintext
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. I repeat step 5 sequentially in the chronological order of the commit IDs, because some commit IDs are consecutive. As shown in Figure 4

I repeat step 5 sequentially in the chronological order of the commit IDs, because some commit IDs are consecutive
Figure 4

7. I can use the convenient syntax below. A conflict occurs. After resolving the code conflict, the first step is to re-stage the modified files (git add .), and the second step is to use the command below to let the cherry-pick process continue. During this process, the vim editor opens (I exit it directly), and I do my editing in PhpStorm. As shown in Figure 5

I can use the convenient syntax below. A conflict occurs. After resolving the code conflict, the first step is to re-stage the modified files (git add .), and the second step is to use the command below to let the cherry-pick process continue. During this process, the vim editor opens (I exit it directly), and I do my editing in PhpStorm
Figure 5
Plaintext
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. I check the commit log, and it meets my expectations. The selected commit IDs have all been incorporated into branch B. As shown in Figure 6

I check the commit log, and it meets my expectations. The selected commit IDs have all been incorporated into branch B
Figure 6

9. If it prompts: The previous cherry-pick is now empty, possibly due to conflict resolution. Then run the command: git cherry-pick –skip.

Plaintext
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

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系