Cherry-pick consecutive commits from another branch
You need a few consecutive fixes from another branch, but you do not want to merge the branch's other work. Apply the commits from oldest to newest, creating a new commit for each one.
Before you start
This recipe assumes the selected commits form a linear, non-merge sequence. Revision ranges walk commit ancestry, so a range that crosses a merge can include commits from both sides. For a merge-heavy history, select and review the commits individually instead.
This example copies the last three commits from a local feature branch onto
main. Fetch the source branch if necessary, then switch to the branch that
should receive the commits:
git fetch origin
git switch main
git status --short
The target branch must have a clean working tree. The source branch and target branch names in this example are illustrative; use your repository's actual branches when following the steps.
Apply the commit range
Inspect the exact commits in application order. This range selects the last
three commits on the linear feature branch:
git log --reverse --oneline feature~3..feature
If the list is the intended linear sequence, apply it:
git cherry-pick feature~3..feature
Git applies each selected commit to the current branch and creates a new commit for each one. It does not move or merge the source branch.
Verify the result
Review the target branch and confirm that the working tree is clean:
git log --oneline --decorate -n 3
git status --short --branch
Run the relevant project tests as well. The commit IDs will differ from the source branch because cherry-picking recreates the changes with the target branch as their new parent.
If a conflict occurs
Git stops at the conflicting commit. Inspect the state, resolve every conflicted file, review the result, stage the resolutions, and resume:
git status
git diff
git add src/example.ts
git diff --check
Replace src/example.ts with each file you resolved before continuing.
To abandon the entire in-progress sequence and return to the state before the cherry-pick started:
If a selected commit is already represented on the target branch, or was
intentionally empty, Git may stop because the cherry-pick is empty. Use
git cherry-pick --skip only after confirming that the commit's result is not
needed; otherwise decide whether to keep the empty commit with the options
documented in the git cherry-pick manual.