From 30eb1e6579559f5fc3127b32c5e9b55b5b418cd6 Mon Sep 17 00:00:00 2001 From: Erik Cervin-Edin Date: Tue, 26 May 2026 12:47:43 +0200 Subject: [PATCH 1/2] 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 -m"", 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 Suggested-by: Phillip Wood Signed-off-by: Erik Cervin-Edin Signed-off-by: Junio C Hamano --- Documentation/git-commit.adoc | 19 ++++---- builtin/commit.c | 34 +++++++------- t/t7500-commit-template-squash-signoff.sh | 56 +++++++++++++++++------ 3 files changed, 69 insertions(+), 40 deletions(-) diff --git a/Documentation/git-commit.adoc b/Documentation/git-commit.adoc index 8329c1034b..61efd29e66 100644 --- a/Documentation/git-commit.adoc +++ b/Documentation/git-commit.adoc @@ -103,20 +103,21 @@ include::diff-context-options.adoc[] The commit created by plain `--fixup=` has a title composed of "fixup!" followed by the title of __, and is recognized specially by `git rebase --autosquash`. The `-m` -option may be used to supplement the log message of the created -commit, but the additional commentary will be thrown away once the -"fixup!" commit is squashed into __ by +or `-F` option may be used to supplement the log message +of the created commit, but the additional commentary will be thrown +away once the "fixup!" commit is squashed into __ by `git rebase --autosquash`. + The commit created by `--fixup=amend:` is similar but its title is instead prefixed with "amend!". The log message of __ is copied into the log message of the "amend!" commit and -opened in an editor so it can be refined. When `git rebase ---autosquash` squashes the "amend!" commit into __, the -log message of __ is replaced by the refined log message -from the "amend!" commit. It is an error for the "amend!" commit's -log message to be empty unless `--allow-empty-message` is -specified. +opened in an editor so it can be refined. The replacement message may +also be supplied directly using `-m` or `-F`, bypassing the +need to open an editor. When `git rebase +--autosquash` squashes the "amend!" commit into __, the log +message of __ is replaced by the refined log message from the +"amend!" commit. It is an error for the "amend!" commit's log message +to be empty unless `--allow-empty-message` is specified. + `--fixup=reword:` is shorthand for `--fixup=amend: --only`. It creates an "amend!" commit with only a log message diff --git a/builtin/commit.c b/builtin/commit.c index 28f6174503..3f1fca2919 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -804,18 +804,18 @@ static int prepare_to_commit(const char *index_file, const char *prefix, if (have_option_m && !fixup_message) { strbuf_addbuf(&sb, &message); hook_arg1 = "message"; - } else if (logfile && !strcmp(logfile, "-")) { + } else if (logfile && !fixup_message && !strcmp(logfile, "-")) { if (isatty(0)) fprintf(stderr, _("(reading log message from standard input)\n")); if (strbuf_read(&sb, 0, 0) < 0) die_errno(_("could not read log from standard input")); hook_arg1 = "message"; - } else if (logfile) { + } else if (logfile && !fixup_message) { if (strbuf_read_file(&sb, logfile, 0) < 0) die_errno(_("could not read log file '%s'"), logfile); hook_arg1 = "message"; - } else if (use_message) { + } else if (use_message && !fixup_message) { const char *buffer; buffer = strstr(use_message_buffer, "\n\n"); if (buffer) @@ -837,20 +837,21 @@ static int prepare_to_commit(const char *index_file, const char *prefix, hook_arg1 = "message"; /* - * Only `-m` commit message option is checked here, as - * it supports `--fixup` to append the commit message. - * - * The other commit message options `-c`/`-C`/`-F` are - * incompatible with all the forms of `--fixup` and - * have already errored out while parsing the `git commit` - * options. + * Only `-m` and `-F` are handled here. `-c`/`-C` are + * incompatible with --fixup and have already errored out + * during option parsing. */ - if (have_option_m && !strcmp(fixup_prefix, "fixup")) + if (have_option_m) { strbuf_addbuf(&sb, &message); - - if (!strcmp(fixup_prefix, "amend")) { - if (have_option_m) - die(_("options '%s' and '%s:%s' cannot be used together"), "-m", "--fixup", fixup_message); + } else if (logfile && !strcmp(logfile, "-")) { + if (isatty(0)) + fprintf(stderr, _("(reading log message from standard input)\n")); + if (strbuf_read(&sb, 0, 0) < 0) + die_errno(_("could not read log from standard input")); + } else if (logfile) { + if (strbuf_read_file(&sb, logfile, 0) < 0) + die_errno(_("could not read log file '%s'"), logfile); + } else if (!strcmp(fixup_prefix, "amend")) { prepare_amend_commit(commit, &sb, &ctx); } } 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[], } if (fixup_message && squash_message) die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup"); - die_for_incompatible_opt4(!!use_message, "-C", + die_for_incompatible_opt3(!!use_message, "-C", !!edit_message, "-c", - !!logfile, "-F", !!fixup_message, "--fixup"); die_for_incompatible_opt4(have_option_m, "-m", !!edit_message, "-c", diff --git a/t/t7500-commit-template-squash-signoff.sh b/t/t7500-commit-template-squash-signoff.sh index 66aff8e097..01c7400136 100755 --- a/t/t7500-commit-template-squash-signoff.sh +++ b/t/t7500-commit-template-squash-signoff.sh @@ -384,18 +384,24 @@ test_expect_success '--fixup=reword: ignores staged changes' ' test_cmp foo actual ' -test_expect_success '--fixup=reword: error out with -m option' ' +test_expect_success 'commit --fixup=reword: works with -m' ' commit_for_rebase_autosquash_setup && - echo "fatal: options '\''-m'\'' and '\''--fixup:reword'\'' cannot be used together" >expect && - test_must_fail git commit --fixup=reword:HEAD~ -m "reword commit message" 2>actual && - test_cmp expect actual + git commit --fixup=reword:HEAD~ -m "reword commit message" && + test_commit_message HEAD <<-EOF + amend! $(git log -1 --format=%s HEAD~2) + + reword commit message + EOF ' -test_expect_success '--fixup=amend: error out with -m option' ' +test_expect_success 'commit --fixup=amend: works with -m' ' commit_for_rebase_autosquash_setup && - echo "fatal: options '\''-m'\'' and '\''--fixup:amend'\'' cannot be used together" >expect && - test_must_fail git commit --fixup=amend:HEAD~ -m "amend commit message" 2>actual && - test_cmp expect actual + git commit --fixup=amend:HEAD~ -m "amend commit message" && + test_commit_message HEAD <<-EOF + amend! $(git log -1 --format=%s HEAD~2) + + amend commit message + EOF ' 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 test_cmp expected actual ' +test_expect_success 'deny to create amend! commit if -m is empty' ' + commit_for_rebase_autosquash_setup && + echo "Aborting commit due to empty commit message body." >expect && + test_must_fail git commit --fixup=amend:HEAD~ -m "" 2>actual && + test_cmp expect actual +' + test_expect_success 'amend! commit allows empty commit msg body with --allow-empty-message' ' commit_for_rebase_autosquash_setup && cat >expected <<-EOF && @@ -468,10 +481,26 @@ test_expect_success '--fixup=reword: give error with pathsec' ' test_cmp expect actual ' -test_expect_success '--fixup=reword: -F give error message' ' - echo "fatal: options '\''-F'\'' and '\''--fixup'\'' cannot be used together" >expect && - test_must_fail git commit --fixup=reword:HEAD~ -F msg 2>actual && - test_cmp expect actual +test_expect_success 'commit --fixup works with -F' ' + commit_for_rebase_autosquash_setup && + echo "message" >msgfile && + git commit --fixup HEAD~ -F msgfile && + test_commit_message HEAD <<-EOF + fixup! $(git log -1 --format=%s HEAD~2) + + message + EOF +' + +test_expect_success 'commit --fixup=reword: works with -F' ' + commit_for_rebase_autosquash_setup && + echo "message from file" >msgfile && + git commit --fixup=reword:HEAD~ -F msgfile && + test_commit_message HEAD <<-EOF + amend! $(git log -1 --format=%s HEAD~2) + + $(cat msgfile) + EOF ' test_expect_success 'commit --squash works with -F' ' @@ -526,8 +555,7 @@ test_expect_success 'invalid message options when using --fixup' ' git add foo && test_must_fail git commit --fixup HEAD~1 --squash HEAD~2 && test_must_fail git commit --fixup HEAD~1 -C HEAD~2 && - test_must_fail git commit --fixup HEAD~1 -c HEAD~2 && - test_must_fail git commit --fixup HEAD~1 -F log + test_must_fail git commit --fixup HEAD~1 -c HEAD~2 ' cat >expected-template < Date: Tue, 26 May 2026 12:47:44 +0200 Subject: [PATCH 2/2] commit: allow -c/-C for all kinds of --fixup The previous commit allowed -m and -F for all --fixup variations. The -c/-C flags were blocked by the same higher-layer incompatibility check that previously caught -F, namely die_for_incompatible_opt4() grouping them with --fixup. Drop --fixup from that check and route the resolved commit through prepare_amend_commit() in the fixup path, mirroring the no-message-source behaviour of --fixup=amend. With this in place, -m/-F/-c/-C all behave consistently across the plain, amend, and reword --fixup forms. Signed-off-by: Erik Cervin-Edin Signed-off-by: Junio C Hamano --- Documentation/git-commit.adoc | 9 ++-- builtin/commit.c | 13 +++-- t/t7500-commit-template-squash-signoff.sh | 60 +++++++++++++++++++++-- 3 files changed, 71 insertions(+), 11 deletions(-) diff --git a/Documentation/git-commit.adoc b/Documentation/git-commit.adoc index 61efd29e66..98c50a3be5 100644 --- a/Documentation/git-commit.adoc +++ b/Documentation/git-commit.adoc @@ -102,8 +102,8 @@ include::diff-context-options.adoc[] + The commit created by plain `--fixup=` has a title composed of "fixup!" followed by the title of __, -and is recognized specially by `git rebase --autosquash`. The `-m` -or `-F` option may be used to supplement the log message +and is recognized specially by `git rebase --autosquash`. The `-m`, +`-F`, `-C`, or `-c` option may be used to supplement the log message of the created commit, but the additional commentary will be thrown away once the "fixup!" commit is squashed into __ by `git rebase --autosquash`. @@ -112,8 +112,9 @@ The commit created by `--fixup=amend:` is similar but its title is instead prefixed with "amend!". The log message of __ is copied into the log message of the "amend!" commit and opened in an editor so it can be refined. The replacement message may -also be supplied directly using `-m` or `-F`, bypassing the -need to open an editor. When `git rebase +also be supplied directly using `-m`, `-F`, or `-C`, bypassing the +need to open an editor, or using `-c` to open the editor pre-populated +with the referenced commit's message. When `git rebase --autosquash` squashes the "amend!" commit into __, the log message of __ is replaced by the refined log message from the "amend!" commit. It is an error for the "amend!" commit's log message diff --git a/builtin/commit.c b/builtin/commit.c index 3f1fca2919..fcf148eb21 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -837,9 +837,9 @@ static int prepare_to_commit(const char *index_file, const char *prefix, hook_arg1 = "message"; /* - * Only `-m` and `-F` are handled here. `-c`/`-C` are - * incompatible with --fixup and have already errored out - * during option parsing. + * `-m`, `-F`, `-C`, and `-c` provide the message body. + * If none was given and this is an amend, use the target + * commit's body instead. */ if (have_option_m) { strbuf_addbuf(&sb, &message); @@ -851,6 +851,11 @@ static int prepare_to_commit(const char *index_file, const char *prefix, } else if (logfile) { if (strbuf_read_file(&sb, logfile, 0) < 0) die_errno(_("could not read log file '%s'"), logfile); + } else if (use_message) { + struct commit *c = lookup_commit_reference_by_name(use_message); + if (!c) + die(_("could not lookup commit '%s'"), use_message); + prepare_amend_commit(c, &sb, &ctx); } else if (!strcmp(fixup_prefix, "amend")) { prepare_amend_commit(commit, &sb, &ctx); } @@ -1341,7 +1346,7 @@ static int parse_and_validate_options(int argc, const char *argv[], die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup"); die_for_incompatible_opt3(!!use_message, "-C", !!edit_message, "-c", - !!fixup_message, "--fixup"); + !!logfile, "-F"); die_for_incompatible_opt4(have_option_m, "-m", !!edit_message, "-c", !!use_message, "-C", diff --git a/t/t7500-commit-template-squash-signoff.sh b/t/t7500-commit-template-squash-signoff.sh index 01c7400136..48e1247d9e 100755 --- a/t/t7500-commit-template-squash-signoff.sh +++ b/t/t7500-commit-template-squash-signoff.sh @@ -492,6 +492,62 @@ test_expect_success 'commit --fixup works with -F' ' EOF ' +test_expect_success 'commit --fixup works with -C' ' + commit_for_rebase_autosquash_setup && + git commit --fixup HEAD~ -C HEAD && + test_commit_message HEAD <<-EOF + fixup! $(git log -1 --format=%s HEAD~2) + + $(get_commit_msg HEAD~) + EOF +' + +test_expect_success 'commit --fixup=amend: works with -c' ' + commit_for_rebase_autosquash_setup && + test_set_editor : && + git commit --fixup=amend:HEAD -c HEAD~ && + test_commit_message HEAD <<-EOF + amend! intermediate commit + + target message subject line + + target message body line 1 + target message body line 2 + EOF +' + +test_expect_success 'commit --fixup=amend:HEAD with -C HEAD and without have the same message' ' + commit_for_rebase_autosquash_setup && + start=$(git rev-parse HEAD) && + + git commit --fixup=amend:HEAD -C HEAD && + git commit --fixup=amend:HEAD -C HEAD && + git log -1 --pretty=%B >with-c && + + git reset --hard "$start" && + test_set_editor : && + git commit --fixup=amend:HEAD && + git commit --fixup=amend:HEAD && + git log -1 --pretty=%B >without-c && + + test_cmp with-c without-c +' + +test_expect_success 'commit --fixup=amend: with -C copies full subject + body of squash commit' ' + commit_for_rebase_autosquash_setup && + git commit --squash HEAD~ -m "inner body" && + echo "extra" >>foo && + git add foo && + git commit --fixup=amend:HEAD -C HEAD && + test_commit_message HEAD <<-EOF + amend! squash! $(git log -1 --format=%s HEAD~3) + + squash! $(git log -1 --format=%s HEAD~3) + + inner body + EOF +' + test_expect_success 'commit --fixup=reword: works with -F' ' commit_for_rebase_autosquash_setup && echo "message from file" >msgfile && @@ -553,9 +609,7 @@ test_expect_success 'invalid message options when using --fixup' ' echo changes >>foo && echo "message" >log && git add foo && - test_must_fail git commit --fixup HEAD~1 --squash HEAD~2 && - test_must_fail git commit --fixup HEAD~1 -C HEAD~2 && - test_must_fail git commit --fixup HEAD~1 -c HEAD~2 + test_must_fail git commit --fixup HEAD~1 --squash HEAD~2 ' cat >expected-template <