Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Merging and Rebasing

git cherry-pick command

Reference for git cherry-pick: copy one or many commits onto the current branch, handle conflicts, use -x, -n, -m for merges and understand the duplicate it creates.

git cherry-pick takes the changes introduced by one or more existing commits and records them as new commits on the branch you are on. It is the way to move a single bug fix from main to a release branch, or to rescue one commit from an abandoned branch without merging everything around it.

The new commit has the same message and the same diff as the original but a different parent, so it has a different id. Git sees the two as unrelated commits that happen to carry the same change; this is why heavy use of cherry-pick can make later merges noisy. Under the hood it runs a three-way merge using the picked commit's parent as the base.

Terminal
git switch release-1.0
git cherry-pick 3f2a1b9              # copy one commit
git cherry-pick A..B                 # copy a range (A excluded)
git cherry-pick -x 3f2a1b9           # record the original id in the message

Try it

Bash Copy commits to another branch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Conflicts, no-commit and empty picks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
git cherry-pick [<options>] <commit>...
git cherry-pick (--continue | --skip | --abort | --quit)
Name Description
<commit>... Commits to copy. Several commits are applied in the order given. A..B copies the commits after A up to B; ^A B is the same.
-x Append a line (cherry picked from commit <id>) to the message. Use it when the original is reachable by others; avoid for private picks.
-n, --no-commit Apply the changes to the index and working tree but do not commit. Several commits can be combined into one this way.
-e, --edit Edit the message before committing.
--ff Fast-forward if the current HEAD is the parent of the picked commit (no new commit is created).
-m <n>, --mainline <n> Needed to pick a merge commit: the parent number (1-based) to treat as the mainline.
-s, --signoff Add a Signed-off-by line.
-X <option>, --strategy Merge strategy and its options, see Merge strategies.
--allow-empty Keep commits that were empty to begin with.
--allow-empty-message Allow a commit with an empty message.
--keep-redundant-commits Keep commits that become empty because the change is already present.
--empty=(drop|keep|stop) What to do with commits that become empty. Default is stop for a single pick.
--continue, --skip, --abort, --quit Control a stopped sequence of picks.

Errors and edge cases: a conflict stops the pick and leaves markers; picking a merge commit without -m fails with is a merge but no -m option was given. A dirty index blocks the command.

Usage notes

  • A copy, not a move. The original commit stays on its branch. After you later merge that branch, Git applies the same change twice; it usually detects that it is already there, but conflicts are possible. git cherry shows which commits were already copied.
  • Order matters. Pick commits oldest first. A fix that builds on an earlier change will conflict if the earlier one is missing.
  • Backporting etiquette. Use -x for picks onto long-lived branches, so a future reader can trace the original.
  • Merge commits. git cherry-pick -m 1 <merge> replays the merge's changes relative to its first parent. It copies the net effect, not the merged history.
  • Rerere. Enable rerere.enabled if you repeat the same picks across several release branches.
  • Alternatives. Moving a whole branch is a rebase; undoing a commit is a revert; taking one file from another branch is git restore --source=<branch> -- <file>.
  • Interrupted sequences. git cherry-pick --abort returns to the state before the first pick; --quit keeps the commits already made and forgets the rest.
  • Versions. --skip and the A..B sequencer behavior need Git 2.x; --empty was added in Git 2.45.