commit: allow -m/-F for all kinds of --fixup

The ability to provide a commit message for git commit --fixup and its variations is limited: * Plain --fixup only allows using the -m flag * The amend/reword --fixup variants only allow supplying the message using an editor For amend/reword, the -m and -F flags are rejected: -m is caught by a die() in prepare_to_commit(), and -F is caught by die_for_incompatible_opt4() which groups -F with --fixup as mutually exclusive. This makes these modes poorly suited for non-interactive workflows -- notably when using AI coding agents. When support to use the -m option was introduced in [1] it was noted that there could be support for other options but at the time the use case was deemed too niche. Later, when the amend suboption was introduced in [2] -m support for amend fixups was discussed but not pursued, and -F was already caught by the higher-layer incompatibility check grouping it with --fixup. The rejections of these options hark back to when --fixup was introduced in [3] and as noted in [1] -- there's nothing inherently preventing support for them. The current patchwork of which flags work with which --fixup variants has no strong logic to it, and allowing all of them simplifies both the code and the interface. Allow -m and -F to supply the message body for all --fixup variations, mirroring the flow of a regular commit. -c and -C, which are blocked by the same incompatibility check, are handled in the next commit. 1. 30884c9afc (commit: add support for --fixup <commit> -m"<extra message>", 2017-12-22) 2. 494d314a05 (commit: add amend suboption to --fixup to create amend! commit, 2021-03-15) 3. d71b8ba7c9 (commit: --fixup option for use with rebase --autosquash, 2010-11-02) Helped-by: Junio C Hamano <gitster@pobox.com> Suggested-by: Phillip Wood <phillip.wood123@gmail.com> Signed-off-by: Erik Cervin-Edin <erik@cervined.in> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Erik Cervin-Edin committed May 26, 2026 at 12:47 UTC 30eb1e6579559f5fc3127b32c5e9b55b5b418cd6
3 files changed +69 -40
Documentation/git-commit.adoc
+10 -9
@@ -103,20 +103,21 @@ include::diff-context-options.adoc[]
103 The commit created by plain `--fixup=<commit>` has a title
104 composed of "fixup!" followed by the title of _<commit>_,
105 and is recognized specially by `git rebase --autosquash`. The `-m`
106 -option may be used to supplement the log message of the created
107 -commit, but the additional commentary will be thrown away once the
108 -"fixup!" commit is squashed into _<commit>_ by
106 +or `-F` option may be used to supplement the log message
107 +of the created commit, but the additional commentary will be thrown
108 +away once the "fixup!" commit is squashed into _<commit>_ by
109 `git rebase --autosquash`.
110 +
111 The commit created by `--fixup=amend:<commit>` is similar but its
112 title is instead prefixed with "amend!". The log message of
113 _<commit>_ is copied into the log message of the "amend!" commit and
114 -opened in an editor so it can be refined. When `git rebase
115 ---autosquash` squashes the "amend!" commit into _<commit>_, the
116 -log message of _<commit>_ is replaced by the refined log message
117 -from the "amend!" commit. It is an error for the "amend!" commit's
118 -log message to be empty unless `--allow-empty-message` is
119 -specified.
114 +opened in an editor so it can be refined. The replacement message may
115 +also be supplied directly using `-m` or `-F`, bypassing the
116 +need to open an editor. When `git rebase
117 +--autosquash` squashes the "amend!" commit into _<commit>_, the log
118 +message of _<commit>_ is replaced by the refined log message from the
119 +"amend!" commit. It is an error for the "amend!" commit's log message
120 +to be empty unless `--allow-empty-message` is specified.
121 +
122 `--fixup=reword:<commit>` is shorthand for `--fixup=amend:<commit>
123 --only`. It creates an "amend!" commit with only a log message
builtin/commit.c
+17 -17
@@ -804,18 +804,18 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
804 if (have_option_m && !fixup_message) {
805 strbuf_addbuf(&sb, &message);
806 hook_arg1 = "message";
807 - } else if (logfile && !strcmp(logfile, "-")) {
807 + } else if (logfile && !fixup_message && !strcmp(logfile, "-")) {
808 if (isatty(0))
809 fprintf(stderr, _("(reading log message from standard input)\n"));
810 if (strbuf_read(&sb, 0, 0) < 0)
811 die_errno(_("could not read log from standard input"));
812 hook_arg1 = "message";
813 - } else if (logfile) {
813 + } else if (logfile && !fixup_message) {
814 if (strbuf_read_file(&sb, logfile, 0) < 0)
815 die_errno(_("could not read log file '%s'"),
816 logfile);
817 hook_arg1 = "message";
818 - } else if (use_message) {
818 + } else if (use_message && !fixup_message) {
819 const char *buffer;
820 buffer = strstr(use_message_buffer, "\n\n");
821 if (buffer)
@@ -837,20 +837,21 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
837 hook_arg1 = "message";
838
839 /*
840 - * Only `-m` commit message option is checked here, as
841 - * it supports `--fixup` to append the commit message.
842 - *
843 - * The other commit message options `-c`/`-C`/`-F` are
844 - * incompatible with all the forms of `--fixup` and
845 - * have already errored out while parsing the `git commit`
846 - * options.
840 + * Only `-m` and `-F` are handled here. `-c`/`-C` are
841 + * incompatible with --fixup and have already errored out
842 + * during option parsing.
843 */
848 - if (have_option_m && !strcmp(fixup_prefix, "fixup"))
844 + if (have_option_m) {
845 strbuf_addbuf(&sb, &message);
850 -
851 - if (!strcmp(fixup_prefix, "amend")) {
852 - if (have_option_m)
853 - die(_("options '%s' and '%s:%s' cannot be used together"), "-m", "--fixup", fixup_message);
846 + } else if (logfile && !strcmp(logfile, "-")) {
847 + if (isatty(0))
848 + fprintf(stderr, _("(reading log message from standard input)\n"));
849 + if (strbuf_read(&sb, 0, 0) < 0)
850 + die_errno(_("could not read log from standard input"));
851 + } else if (logfile) {
852 + if (strbuf_read_file(&sb, logfile, 0) < 0)
853 + die_errno(_("could not read log file '%s'"), logfile);
854 + } else if (!strcmp(fixup_prefix, "amend")) {
855 prepare_amend_commit(commit, &sb, &ctx);
856 }
857 } else if (!stat(git_path_merge_msg(the_repository), &statbuf)) {
@@ -1338,9 +1339,8 @@ static int parse_and_validate_options(int argc, const char *argv[],
1339 }
1340 if (fixup_message && squash_message)
1341 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup");
1341 - die_for_incompatible_opt4(!!use_message, "-C",
1342 + die_for_incompatible_opt3(!!use_message, "-C",
1343 !!edit_message, "-c",
1343 - !!logfile, "-F",
1344 !!fixup_message, "--fixup");
1345 die_for_incompatible_opt4(have_option_m, "-m",
1346 !!edit_message, "-c",
t/t7500-commit-template-squash-signoff.sh
+42 -14
@@ -384,18 +384,24 @@ test_expect_success '--fixup=reword: ignores staged changes' '
384 test_cmp foo actual
385 '
386
387 -test_expect_success '--fixup=reword: error out with -m option' '
387 +test_expect_success 'commit --fixup=reword: works with -m' '
388 commit_for_rebase_autosquash_setup &&
389 - echo "fatal: options '\''-m'\'' and '\''--fixup:reword'\'' cannot be used together" >expect &&
390 - test_must_fail git commit --fixup=reword:HEAD~ -m "reword commit message" 2>actual &&
391 - test_cmp expect actual
389 + git commit --fixup=reword:HEAD~ -m "reword commit message" &&
390 + test_commit_message HEAD <<-EOF
391 + amend! $(git log -1 --format=%s HEAD~2)
392 +
393 + reword commit message
394 + EOF
395 '
396
394 -test_expect_success '--fixup=amend: error out with -m option' '
397 +test_expect_success 'commit --fixup=amend: works with -m' '
398 commit_for_rebase_autosquash_setup &&
396 - echo "fatal: options '\''-m'\'' and '\''--fixup:amend'\'' cannot be used together" >expect &&
397 - test_must_fail git commit --fixup=amend:HEAD~ -m "amend commit message" 2>actual &&
398 - test_cmp expect actual
399 + git commit --fixup=amend:HEAD~ -m "amend commit message" &&
400 + test_commit_message HEAD <<-EOF
401 + amend! $(git log -1 --format=%s HEAD~2)
402 +
403 + amend commit message
404 + EOF
405 '
406
407 test_expect_success 'consecutive amend! commits remove amend! line from commit msg body' '
@@ -432,6 +438,13 @@ test_expect_success 'deny to create amend! commit if its commit msg body is empt
438 test_cmp expected actual
439 '
440
441 +test_expect_success 'deny to create amend! commit if -m is empty' '
442 + commit_for_rebase_autosquash_setup &&
443 + echo "Aborting commit due to empty commit message body." >expect &&
444 + test_must_fail git commit --fixup=amend:HEAD~ -m "" 2>actual &&
445 + test_cmp expect actual
446 +'
447 +
448 test_expect_success 'amend! commit allows empty commit msg body with --allow-empty-message' '
449 commit_for_rebase_autosquash_setup &&
450 cat >expected <<-EOF &&
@@ -468,10 +481,26 @@ test_expect_success '--fixup=reword: give error with pathsec' '
481 test_cmp expect actual
482 '
483
471 -test_expect_success '--fixup=reword: -F give error message' '
472 - echo "fatal: options '\''-F'\'' and '\''--fixup'\'' cannot be used together" >expect &&
473 - test_must_fail git commit --fixup=reword:HEAD~ -F msg 2>actual &&
474 - test_cmp expect actual
484 +test_expect_success 'commit --fixup works with -F' '
485 + commit_for_rebase_autosquash_setup &&
486 + echo "message" >msgfile &&
487 + git commit --fixup HEAD~ -F msgfile &&
488 + test_commit_message HEAD <<-EOF
489 + fixup! $(git log -1 --format=%s HEAD~2)
490 +
491 + message
492 + EOF
493 +'
494 +
495 +test_expect_success 'commit --fixup=reword: works with -F' '
496 + commit_for_rebase_autosquash_setup &&
497 + echo "message from file" >msgfile &&
498 + git commit --fixup=reword:HEAD~ -F msgfile &&
499 + test_commit_message HEAD <<-EOF
500 + amend! $(git log -1 --format=%s HEAD~2)
501 +
502 + $(cat msgfile)
503 + EOF
504 '
505
506 test_expect_success 'commit --squash works with -F' '
@@ -526,8 +555,7 @@ test_expect_success 'invalid message options when using --fixup' '
555 git add foo &&
556 test_must_fail git commit --fixup HEAD~1 --squash HEAD~2 &&
557 test_must_fail git commit --fixup HEAD~1 -C HEAD~2 &&
529 - test_must_fail git commit --fixup HEAD~1 -c HEAD~2 &&
530 - test_must_fail git commit --fixup HEAD~1 -F log
558 + test_must_fail git commit --fixup HEAD~1 -c HEAD~2
559 '
560
561 cat >expected-template <<EOF