git-cherry-pick - Linux


Overview

git-cherry-pick allows selective merging of individual commits from one branch onto another. It is valuable for incorporating targeted changes or fixing specific issues without merging or rebasing an entire branch.

Syntax

git cherry-pick [options] <commit-ish>

Options/Flags

  • -n, –no-commit: Dry run mode. Do not commit the changes.
  • -x, –signoff: Sign the commit.
  • -e, –edit: Open the commit message in an editor before committing.
  • -m, –mainline: Integrate the commit as a mainline commit instead of a merge commit.
  • -s, –strategy=: Specify the merge strategy, e.g. recursive, resolve.

Examples

  • Cherry-pick a single commit:
git cherry-pick <commit-hash>
  • Apply multiple commits in sequence:
git cherry-pick <commit-hash1> <commit-hash2> ...
  • Cherry-pick a commit with an amended commit message:
git cherry-pick -e <commit-hash>

Common Issues

  • Conflicts during cherry-pick: Resolve conflicts using git mergetool or git add/commit.
  • Commit was reverted: Use git cherry-pick -x to sign off on the commit message and prevent merge conflicts.
  • Cherry-pick in the wrong place: If the commit was applied to the wrong branch, use git reset --hard to undo it.

Integration

  • Use with git-rebase: Cherry-pick can be used in a git rebase workflow to apply a series of commits sequentially.
  • Apply cherry-pick to a non-branch target: Use git checkout -b <branch-name> to create a temporary branch and cherry-pick the desired commits.

Related Commands