2

I am trying to cherry-pick a commit from one branch to another. Consider the below scenario.

Branch A -> commit1 -> commit message "12345 Hello World"

I want to add a new message at the beginning of the commit message while doing cherry-pick. So after cherry-pick it should look like,

Branch B -> commit2 -> commit message "98765 ........"

Here 98765 is the extra message I want to add. From Git documentation I found a command "git cherry-pick --edit" but I didn't find any example to understand its proper use.

halfer
  • 19,824
  • 17
  • 99
  • 186

3 Answers3

2

There isn't much to explain about git cherry-pick --edit. If you cherry pick with the --edit option, Git will bring up the commit message editor window before making the commit. There, you may customize the commit message however you want.

As to the logistics of why this is possible, when you cherry pick a commit you actually are making a new commit. So there is nothing extra to prevent you from using any commit message you want.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thanks Tim. What I really need is to provide the commit message in the command itself without bringing the commit message editor. – Kailash Patra Jul 03 '18 at 12:35
  • @KailashPatra I don't know how to do that, and there does not appear to be any built in options for doing this. But, a cherry pick may result in a merge conflict, in which case you would have to manually resolve and commit anyway. Actually, I don't like the idea of automatically committing in general. – Tim Biegeleisen Jul 03 '18 at 12:42
  • To have `git cherry-pick` use a prepared message, use `--edit` as usual, but provide a temporary editor that consists of a script that copies the prepared message over top of the message to be edited. For instance: `GIT_EDITOR=/tmp/replace.sh git cherry-pick --edit ` (where you have first created `/tmp/replace.sh` as the script that overwrites the file given on its command line with the chosen message). – torek Jul 03 '18 at 14:23
0

From comments, what you want is for the edit to occur automatically, rather than for git to present you with the commit message in an editor window.

To do this, write a script that edits the message in the required way. Your script will receive a filename as its first command-line argument, and it must update that file in place. For example you could use a perl script similar to:

open my $fh, '+<', $ARGV[0] or die;

local $/ = undef;
my $a = <$fh>;

seek $fh,0,0;
print $fh "new content $a";

close $fh;

Then you would specify this script as the editor to be used by your cherry-pick cmmand

git -c core.editor="perl /path/to/your/script.pl" cherry-pick --edit <commit>
Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
0

Thank you everyone for your quick response. I found the below way which helps my case. It will be done in two steps.

  1. Cherry-pick without committing to the target using -n.

    git cherry-pick -n <hash>
    
  2. Then perform commit action.

    git commit -m <message>
    
halfer
  • 19,824
  • 17
  • 99
  • 186