24

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 ;)

Harald Nordgren
  • 11,693
  • 6
  • 41
  • 65
  • Well I was not clear with claims you posted so I run a test and found that 'git cherry-pick --continue' freezes until merge conflicts are resolved. So how do you resolve them ? – Zildyan Nov 18 '16 at 13:54
  • I resolve the conflicts with `git add .` in the project root. I want to accept all changes, then run `git cherry-pick --continue`. – Harald Nordgren Nov 18 '16 at 14:06
  • @HaraldNordgren I had the same problem with `git rebase` – Joseph K. Strauss Jun 30 '17 at 02:17

3 Answers3

34

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.)

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775
  • I prefer the fits option (using `-c`) because there are no side effects. In addition, it can easily be turned into an alias. – Joseph K. Strauss Jun 30 '17 at 02:17
  • 1
    @JosephK.Strauss: note that you can use a shell alias to set `GIT_EDITOR`, e.g., `alias foo='GIT_EDITOR=true git cherry-pick --continue'` in bash, or via a Git alias that uses `!` to invoke the shell. However, if `-c core.editor=true` suffices, that's fine too. – torek Jun 30 '17 at 04:13
  • this tricks also works fine in an interactive rebase: `git -c core.editor=true rebase --continue` – hoijui May 15 '19 at 07:11
  • I was using Git in Termux via Android tablet while on vacation. Ran into this problem and was very frustrated for about 5 minutes until finding this answer. – silencedogood Sep 10 '22 at 21:25
  • 1
    @silencedogood: I know nothing of Termux but it's handy to learn to use old-style "line editors" like `ed` and `ex`... – torek Sep 10 '22 at 21:31
2

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
Zildyan
  • 1,261
  • 9
  • 11
0

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250