Skip to content
Latchkey

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
fi

In 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →