0

When we use git cherry-pick -x <commit>, it records the source commit in the commit message as follows (cherry picked from commit 5f2662a29efaa91af6b356df2d5d1df69017cac1)

I have a bunch of commits which I need to keep migrating from one base branch to the next. If I use cherry-pick -x, it keeps adding the latest source commit to the message.

Suppose I have used cherry-pick -x to migrate the commit twice, my commit message looks like below:

commit fb3bead084edc27267219d9a8e088ac613159f69 (my-new-branch)
Author: ----
Date:   Thu Feb 17 15:29:15 2022 +0530

    Test commit 1

    (cherry picked from commit 5f2662a29efaa91af6b356df2d5d1df69017cac1)
    (cherry picked from commit 11c6ca55b06cbf333fff9a3cfd50c6617adc2e77)

Is there way make cherry-pick -x overwrite the 'cherry picked from' message if there is already one? I want to record only the most recent source commit.

rks
  • 542
  • 1
  • 3
  • 13
  • There is not; you would have to automate that yourself (or always cherry pick from the original commit instead of a previously cherry-picked commit). – larsks Mar 01 '22 at 12:10
  • Maybe off-topic, but I want to point out the pitfall in the chained cherry-picks. Cherry-pick A1 from branch1 to branch2 as A2, and then cherry-pick A2 from branch2 to branch3 as A3, and so on until we get AN on branchN. From A1 to AN, some of the diffs could get lost. Image that A1 adds two files foo.txt and bar.txt. On branch3 bar.txt has been already introduced by someone else. When A2 is cherry-picked to branch3, the result commit A3's patch will only contain "adding foo.txt". During the cherry-pick, it does not raise conflicts as there isn't any. It's better to always cherrypick A1. – ElpieKay Mar 01 '22 at 12:36

1 Answers1

1

No, that's the way it works.

But you have alternatives:

  1. You can use the --edit option (thanks @joanis for pointing out in the comments)

With this option, git cherry-pick will let you edit the commit message prior to committing.

  1. You can use the --no-commit option and change the message before actually committing it.

Usually the command automatically creates a sequence of commits. This flag applies the changes necessary to cherry-pick each named commit to your working tree and the index, without making any commit. In addition, when this option is used, your index does not have to match the HEAD commit. The cherry-pick is done against the beginning state of your index.

This is useful when cherry-picking more than one commits' effect to your index in a row.

kapsiR
  • 2,720
  • 28
  • 36
  • 1
    Or the `-e` / `--edit` option to have cherry-pick bring up the editor to edit the message right away. – joanis Mar 01 '22 at 14:40