Sometimes you just want a few patches from a topic branch, not the entire thing. If a bug fix was implemented that would help everyone working out of the master branch, but the rest of the topic branch isn’t acceptable, git cherry-pick is the way to go.

The basic usage for git cherry-pick is git cherry-pick <commit>. This will apply <commit> on top of your current branch.

git cherry-pick has some useful options:

  • -e|--edit
  • -x
  • -n|--no-commit
  • -m <parent-number>

-e lets you edit the commit message before the patch is commited.

-x adds a note to the commit message explaining where it came from originally. This is helpful if you’re backporting a feature or fix from one branch to another.

-n applies the changes but doesn’t actually create a commit. This can be really helpful if you’re combining multiple commits from different places (it can be simpler than using git rebase).

-m <parent-number> lets you cherry pick from a merge. If you tell git cherry-pick which parent is the “mainline”, it can calculate the commit’s impact (its diff) relative to the mainline and apply that as a patch against your current branch.

References:

Advertisements