Harald Nordgren
Harald Nordgren

Reputation: 12391

git cherry-pick --continue, '--no-edit' option?

I'm writing a script for migrating git repos. On cherry-pick conflicts I run

git add .
git cherry-pick --continue

This brings up vim, prompting me to save the commit message and freezes the script. I am looking for a command-line option like --no-edit or --porcelain to get around this.

Ugly terminal hacks might be welcomed as well ;)

Upvotes: 28

Views: 12863

Answers (4)

Wok
Wok

Reputation: 5313

As of January 2024, the command which you mentioned in your question works as intended:

git cherry-pick --continue --no-edit

This was mentioned in this answer. Be wary though that rebase needs a different approach:

git -c core.editor=true rebase --continue

Upvotes: 2

VonC
VonC

Reputation: 1324606

From torek's answer:

git -c core.editor=true cherry-pick --continue

With Git 2.32 (Q2 2021), tihs is no longer needed.

git cherry-pick/revert with or without --[no-]edit did not spawn the editor as expected (e.g. "revert --no-edit" after a conflict still asked to edit the message), which has been corrected with Git 2.32 (Q2 2021).

See commit 39edfd5 (31 Mar 2021) by Elijah Newren (newren).
(Merged by Junio C Hamano -- gitster -- in commit 82fd285, 08 Apr 2021)

sequencer: fix edit handling for cherry-pick and revert messages

Reported-by: Renato Botelho
Signed-off-by: Elijah Newren
Reviewed-by: Johannes Schindelin

save_opts() should save any non-default values.
It was intended to do this, but since most options in struct replay_opts default to 0, it only saved non-zero values.
Unfortunately, this does not always work for options.edit.
Roughly speaking, options.edit had a default value of 0 for cherry-pick but a default value of 1 for revert.
Make save_opts() record a value whenever it differs from the default.

options.edit was also overly simplistic; we had more than two cases.
The behavior that previously existed was as follows:

Non-conflict commits    Right after Conflict
Edit iff isatty(0)      Edit (ignore isatty(0))
No edit                 See above
Edit (ignore isatty(0)) See above
(*)                     See above

(*) Before stopping for conflicts, No edit is the behavior.  After
    stopping for conflicts, the --no-edit flag is not saved so see
    the first two rows.

However, the expected behavior is:

Non-conflict commits    Right after Conflict
Edit iff isatty(0)      Edit iff isatty(0)
No edit                 Edit iff isatty(0)
Edit (ignore isatty(0)) Edit (ignore isatty(0))
No edit                 No edit

In order to get the expected behavior, we need to change options.edit to a tri-state: unspecified, false, or true.

  • When specified, we follow what it says.
  • When unspecified, we need to check whether the current commit being created is resolving a conflict as well as consulting options.action and isatty(0).
    While at it, add a should_edit() utility function that compresses options.edit down to a boolean based on the additional information for the non-conflict case.

continue_single_pick() is the function responsible for resuming after conflict cases, regardless of whether there is one commit being picked or many.
Make this function stop assuming edit behavior in all cases, so that it can correctly handle !isatty(0) and specific requests to not edit the commit message.

Upvotes: 1

torek
torek

Reputation: 488223

As Zildyan said in his answer, you will need to resolve all the conflicts before doing git add. Therefore, you should not make this fully automated.

That said, to skip editing the commit message, you can simply set your editor to a command that does nothing and reports success. The ideal one on Unix-like systems is the true command. Hence:

git -c core.editor=true cherry-pick --continue

will do the trick. (You can also use any of the environment variables GIT_EDITOR, VISUAL, or EDITOR; and in fact, if any of those are set, you must use them rather than core.editor since the sequence is: use $GIT_EDITOR if that is set; else use $VISUAL if that is set; else use $EDITOR if that is set; else use core.editor if that is set; else use whatever is built in to this version of Git.)

Upvotes: 36

user4046031
user4046031

Reputation:

You can use:

git cherry-pick <sha1> --no-commit

after resolving conflict you can commit it from script.

Ofcourse you can set cherry-pick strategy-options to automatically resolve conflicts by accepting code from ours/theirs

Without that you'll get standard git markup of conflict

+<<<<<<< HEAD
         some code
+||||||| parent of 4d64ec6... test commit
+        first version code
+=======
+        second version code
+>>>>>>> 4d64ec6... test commit

Upvotes: 2

Related Questions