Git cherry-pick

The git cherry-pick command lets you pick a specific commit from one branch and apply it to another branch. It’s handy when you need to move individual changes without merging entire branches.



Tutorials dojo strip

Initial Example

$ git cherry-pick <commit-hash>




CHERRY-PICK Options

OptionDescription
<commit>…​Specifies the commits to cherry-pick. No traversal is done by default, behaving as if the --no-walk option was specified. Note that specifying a range will feed all <commit>…​ arguments to a single revision walk.
-e, --editAllows you to edit the commit message before committing.
--cleanup=<mode>Determines how the commit message will be cleaned up before being passed to the commit machinery. For example, using scissors appends scissors to MERGE_MSG in case of a conflict.
-xAdds a line to the commit message indicating which commit this change was cherry-picked from, useful for publicly visible branches.
-rPreviously disabled the default -x behavior, now it is a no-op as -x is not the default.
-m <parent-number>, --mainline <parent-number>Specifies the parent number of the mainline to replay the change relative to the specified parent, useful for cherry-picking a merge.
-n, --no-commitApplies changes to the working tree and index without making any commits. Useful for applying multiple commits in sequence.
-s, --signoffAdds a Signed-off-by trailer at the end of the commit message.
-S[<keyid>], --gpg-sign[=<keyid>], --no-gpg-signGPG-signs commits. If <keyid> is specified, it defaults to the committer identity. --no-gpg-sign counteracts any prior --gpg-sign or the commit.gpgSign configuration.
--ffPerforms a fast forward if the current HEAD is the parent of the cherry-picked commit.
--allow-emptyAllows empty commits to be preserved automatically in a cherry-pick, useful for initially empty commits.
--allow-empty-messageAllows cherry-picking commits with empty messages.
--empty=(drop|keep|stop)Specifies how to handle redundant commits: drop removes the commit, keep preserves it, and stop stops the cherry-pick for review. The default is stop.
--keep-redundant-commitsDeprecated synonym for --empty=keep.
--strategy=<strategy>Uses the given merge strategy, specified only once.
-X<option>, --strategy-option=<option>Passes the merge strategy-specific option to the merge strategy.
--rerere-autoupdate, --no-rerere-autoupdateControls whether the rerere mechanism updates the index after resolving conflicts. --no-rerere-autoupdate is useful for reviewing resolutions before committing.




Examples

1. Simple Example

Suppose you have a feature-branch and you want to apply a specific commit from this branch to main-branch. Here’s how:

  1. Switch to main-branch: $ git checkout main-branch
  2. Cherry-pick the commit: $ git cherry-pick <commit-hash>

Just replace <commit-hash> with the actual hash of the commit you want to apply. This will copy the commit from feature-branch to main-branch.

2. Cherry-Pick Multiple Commits

If you need to apply multiple commits, you can specify each commit hash like this:

$ git cherry-pick <commit-hash1> <commit-hash2> <commit-hash3>

This will copy the specified commits to the current branch.

3. Cherry-Pick a Range of Commits

You can also copy a range of commits using this syntax:

$ git cherry-pick <start-commit-hash>..<end-commit-hash>

Just replace <start-commit-hash> with the hash of the first commit in the range and <end-commit-hash> with the hash of the last commit. This will apply all commits in the specified range to the current branch.

Scroll to Top