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
# 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.
Using this in CI
CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.
- uses: actions/checkout@v4
with:
fetch-depth: 0 # history, tags, and git describe all need this
- run: |
git rev-parse --is-shallow-repository # expect false
git rev-parse --abbrev-ref HEAD # prints HEAD when detachedKey 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.