thiagowfx's avatar

¬ just serendipity 🍀 (not just serendipity)

git: partially cherry-pick a commit

• 123 words • 1 min • updated

OR: “cherry-pick a cherry-pick”.

Adam Johnson:

git cherry-pick allows you to copy a commit another branch to your current one.

[…]

One limitation with a straight-up git cherry-pick, as above, is that it always copies the entire commit. If a commit contains multiple changes, but you only want one of them, you’ll need to undo the undesired cherry-picked changes. This can be tedious and error-prone.

The trick is to use the -n (--no-commit) argument to prevent changes from being committed right away.

shell
% git cherry-pick -n {branch or SHA}

This happens to be the default in mercurial with hg graft.

As such, another possibility is to create an alias:

ini
[alias]
  graft = cherry-pick -n
shell
% git graft {branch or SHA}