Skip to content

CherryPicking

git cherry-pick is a powerful Git command used to apply individual commits from one branch to another. It allows you to select specific commits and apply them to the current branch, effectively incorporating changes from one branch into another.

Syntax

git cherry-pick <commit>
  • <commit>: The commit hash or reference of the commit you want to cherry-pick.

Options

  • -e or --edit: Allows you to edit the commit message before committing the cherry-picked commit.
  • -n or --no-commit: Applies the changes from the commit but does not create a new commit automatically. Useful for cherry-picking multiple commits together.
  • -x or --signoff: Adds a "Signed-off-by" line to the commit message, indicating authorship.
  • -m parent-number or --mainline parent-number: Specifies the parent number when cherry-picking a merge commit. Default is 1 (first parent).
  • -s or --strategy <strategy>: Specifies the merge strategy to use when resolving conflicts. Options include recursive (default), resolve, ours, theirs, etc.

Examples

1. Cherry-picking a Single Commit

git cherry-pick abc123
This command applies the changes from the commit with the hash abc123 to the current branch.

2. Cherry-picking a Range of Commits

git cherry-pick abc123..def456
This command applies the changes from all commits in the range abc123 to def456 to the current branch.

3. Cherry-picking with Edit

git cherry-pick -e abc123
This command applies the changes from the commit abc123 to the current branch and allows you to edit the commit message before committing.

4. Cherry-picking without Committing

git cherry-pick -n abc123
This command applies the changes from the commit abc123 to the current branch but does not create a new commit automatically. You can then manually commit the changes using git commit.

5. Cherry-picking with Merge Strategy

git cherry-pick -s -X theirs abc123
This command applies the changes from the commit abc123 to the current branch and resolves conflicts by favoring the changes from the cherry-picked commit (theirs strategy).

Use Cases

  • Porting Fixes: Cherry-pick commits containing bug fixes or feature enhancements from one branch to another, such as from a development branch to a stable release branch.
  • Reverting Changes: Use cherry-pick to revert specific changes made in one branch from another branch without reverting the entire commit.
  • Selectively Applying Changes: Apply only certain changes from a commit instead of the entire commit by using git cherry-pick -n followed by manual modifications.

git cherry-pick is a versatile command that allows you to apply specific commits from one branch to another. By understanding its options and usage examples, you can effectively incorporate changes from one branch into another, facilitating collaboration and code management in your Git workflow.