git cherry-选取
Applying 特定提交使用 git cherry-选取 — moving individual 提交之间分支.
你将学到
- 如何 apply 单个提交从 one 分支到 another
- 当 cherry-选取是 useful 对比合并 or rebase
- Cherry-选取多个提交 and 范围
- Handling cherry-选取 conflicts
概念讲解
What git cherry-pick Does
git cherry-选取 takes a commit from one branch and applies it as a new commit on your current branch. This lets you grab individual commits without merging an entire branch.
git checkout main
git cherry-pick abc1234
This takes commit abc1234 (from any branch) and creates a new commit with the same changes on 主要.
When to Use cherry-pick
Cherry-pick is useful when you need specific commits without bringing everything else:
- Hotfix: a bug fix on
developneeds to go to主要immediately, without the other unfinished features - Backporting: a fix on the new version needs to be applied to the old version branch
- Selective merge: you want one commit from a colleague's branch but not the rest
- Recovery: a commit ended up on the wrong branch and needs to be moved
Branch A: A---B---C---D---E
Branch B: X---Y---Z
After git cherry-选取 C on Branch B:
Branch A: A---B---C---D---E
Branch B: X---Y---Z---C'
C' is a new commit with the same changes as C but a different hash.
Cherry-Picking Multiple Commits
# Cherry-pick specific commits
git cherry-pick abc1234 def5678 ghi9012
# Cherry-pick a range (inclusive, oldest first)
git cherry-pick A..E
# applies B, C, D, E (not A — the range is exclusive on the start)
# Cherry-pick a range (inclusive on both ends)
git cherry-pick A^..E
# applies A, B, C, D, E
The order matters: cherry-pick applies commits in the order you specify (or chronological order for ranges).
Handling Conflicts
If the commit cannot be applied cleanly:
git cherry-pick abc1234
# CONFLICT (content): Merge conflict in login.js
# Resolve the conflict, then:
git add login.js
git cherry-pick --continue
# To skip this commit:
git cherry-pick --skip
# To abort:
git cherry-pick --abort
Cherry-Pick Options
# Apply the commit but do not commit (leave changes staged)
git cherry-pick --no-commit abc1234
# or:
git cherry-pick -n abc1234
# Edit the commit message before committing
git cherry-pick -e abc1234
# Apply the commit with the original author info (not your name)
git cherry-pick -x abc1234
# adds "(cherry 选取从 提交 ...)" to the message
cherry-pick vs merge vs rebase
git 合并— brings ALL commits from a branch, with a merge commitgit rebase— replays ALL your commits on top of another branchgit cherry-选取— brings SPECIFIC commits, one at a time
Cherry-pick is the scalpel; merge and rebase are the broad strokes.
The Downside of cherry-pick
Cherry-picking creates duplicate commits (same changes, different hashes). If you later merge the source branch, Git may have trouble recognizing that the changes are already applied, potentially causing conflicts.
Use cherry-pick sparingly — it is a tool for specific situations, not a daily workflow. For routine integration, use merge or rebase.
A Common Pattern: Hotfix
# Bug fix is committed on develop
git checkout develop
git commit -m "fix: urgent security patch"
# Apply the same fix to main immediately
git checkout main
git cherry-pick develop # cherry-picks the last commit on develop
git push origin main
# Later, when develop merges into main, Git may recognize the duplicate
# or you may need to handle it — this is the trade-off of cherry-pick示例代码
# --- Cherry-pick a hotfix from develop to main ---
git checkout develop
git log --oneline -3
# c3d2e1f fix: security vulnerability in auth
# b2c3d4e feat: add user settings page
# a1b2c3d feat: add profile photos
git checkout main
git cherry-pick c3d2e1f
# applies the security fix to main
git push origin main
# --- Cherry-pick multiple specific commits ---
git cherry-pick a1b2c3d c3d2e1f
# --- Cherry-pick a range of commits ---
git cherry-pick b2c3d4e..c3d2e1f
# applies the commits between (exclusive start)
# --- Cherry-pick without committing ---
git cherry-pick --no-commit abc1234
# changes are staged, you can modify before committing
git commit -m "fix: apply security patch with modifications"
# --- Cherry-pick with conflict resolution ---
git cherry-pick abc1234
# CONFLICT in login.js
# ... resolve in editor ...
git add login.js
git cherry-pick --continue
# --- Abort a problematic cherry-pick ---
git cherry-pick --abort
# --- Cherry-pick and edit the commit message ---
git cherry-pick -e abc1234
# editor opens to change the message
# --- Mark the cherry-picked commit with its origin ---
git cherry-pick -x abc1234
# message includes: "(cherry picked from commit abc1234...)"
Shows cherry-选取 hotfix 从 develop 到主要, 多个提交, 范围, --否-提交 for staging 不使用 committing, conflict resolution, aborting, 编辑消息, and -x flag 到记录原点. hotfix 模式是 大多数常见 cherry-选取 use 情况.
动手试一试
- Code Diff
Review 提交's 更改之前 cherry-选取它.
- CI/CD YAML Generator
Cherry-选取 hotfixes 应该触发 CI 到验证 fix.
常见错误
错误写法
Cherry-选取提交 and later 合并源 分支, causing duplicate 更改
正确做法
Cherry-选取创建新 提交使用相同更改但 不同 hash. 如果你 later 合并源 分支, 原始提交 comes 也, potentially causing conflicts or duplicate 更改. Use cherry-选取 for temporary fixes (如 hotfixes) 哪里源 分支将 不是 合并, or 是 prepared 到 handle duplicate 期间 later 合并.
错误写法
Cherry-选取提交 in 错误顺序
正确做法
Cherry-选取 applies 提交 in 顺序你 specify. 如果提交 B depends on 提交 , 你必须 cherry-选取首先, 然后 B. For 范围 (..E), Git applies 它们 in chronological 顺序. 如果你 获取 conflicts, 它可能是 因为 later 提交 depends on earlier one 你有 不 applied yet.
相关资源
相关速查表
- Git Cheatsheet
cherry-选取, cherry-选取 --continue, and --abort commands.
练习 git cherry-选取
3 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。