git cherry-pick Command in CI
git cherry-pick replays the changes from one or more commits onto the current branch.
Backport automation cherry-picks fixes from a main branch onto release branches. The non-interactive flags let CI either commit the pick or stop cleanly on conflict.
Common flags
<commit>- apply the named commit (or a range with A..B)-x- append a "cherry picked from commit ..." line for traceability--no-commit/-n- apply changes to the index without committing--continue- resume after resolving conflicts--abort- cancel the cherry-pick and restore the prior state-m <parent>- pick a merge commit, selecting the mainline parent
Example
shell
# Backport a fix to a release branch, recording its origin
git switch release/1.x
if ! git cherry-pick -x "${FIX_SHA}"; then
git cherry-pick --abort
echo "Conflict: backport needs manual attention" >&2
exit 1
fiIn CI
Always handle conflicts in scripted cherry-picks: on failure, run --abort and surface a clear message rather than leaving the branch mid-pick. The -x flag adds traceability so reviewers can find the original commit.
Key takeaways
- git cherry-pick -x records the source commit, aiding backport audits.
- In CI, catch failures and run --abort so the branch never ends mid-pick.
- Use -m to cherry-pick a merge commit by choosing its mainline parent.
Related guides
git merge-base Command: Find Common Ancestor in CIgit merge-base finds the best common ancestor of two commits. Reference for computing the PR base SHA that dr…
git reset Command: Move HEAD in CIgit reset moves HEAD and optionally the index and working tree. Reference for --hard and --soft to roll back…
git apply Command: Apply Patches in CIgit apply applies a patch to the working tree or index. Reference for --check, --3way, and --index used to ap…
git log Command: Inspect History in CIgit log shows commit history. Reference for --oneline, -n, and --format to extract commit messages and author…