6

Given a commit with the message "foo", i.e. with only a summary part, I do git cherry-pick -x the_commit. The result is a new commit with message

foo
(cherry picked from commit eb42a6475d2c2e4fff7a1b626ce6e27eec21e886)
Now that's not good, because it is a two-line summary, which seems like a bug in git.

But how can I make git make the comment look like below without editing the comment manually?

foo

(cherry picked from commit eb42a6475d2c2e4fff7a1b626ce6e27eec21e886)
StellarVortex
  • 576
  • 4
  • 19

1 Answers1

7

You're right that it seems like an oversight. You could send an email to the git mailing list and see what they think! For now you'll have to handle it yourself, though.

The good way to deal with this would be to avoid it altogether: make the original commit message good. If it's already multi-line, with the blank line in there, the appended line from the cherry-pick will not screw up the format.

To work around it, given that the cherry-picked commit has a one-line message, as you say, you can use the -e option for cherry-pick. If you're using Vim, worst case you have to hit ggo<Esc>ZZ to take care of it.

Or you could write a prepare-commit-msg hook. All you should need in it is:

#!/bin/bash
sed -i '2s/^(cherry picked/\n&' "$1"
Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • I can't make the original committers (i.e. other people) format their messages in that way. To edit the message manually, `git commit -x -e` seems like a better solution. – StellarVortex Nov 16 '10 at 09:02
  • In my previous comment, I meant `git cherry-pick -x -e`. (Can't edit anymore.) – StellarVortex Nov 18 '10 at 07:39
  • @stellar: Oh, duh, you're right. I forgot cherry-pick had that option. Glad you got a response from the mailing list, though! – Cascabel Nov 18 '10 at 07:44
  • @stellar: One other thought - you could use a prepare-commit-msg hook to rewrite the message. Shouldn't be too bad - just check if line two starts with "(cherry picked", and if so, insert a blank line. – Cascabel Nov 18 '10 at 07:56
  • Still not fixed in Git 1.8.1.2 -- Apparently this finally gets fixed in the upcoming Git version: http://git.661346.n2.nabble.com/PATCH-cherry-pick-x-improve-handling-of-one-liner-commit-messages-tp7581046p7581055.html – kfunk Apr 22 '13 at 10:00