Commit

6257588252ec0196c3d4567bec2921ed4e68d43e

commit: refuse to amend during conflict resolution

Running `git commit --amend` during conflict resolution is an ugly
foot-gun.  For many years, we have rejected amending during conflict
resolution in the middle of
  - a merge
  - a cherry-pick
However, this was never extended to other operations that can also
produce conflicts:
  - an `am` operation
  - a revert
  - a rebase

Extend it to handle these other cases now.

Extending to `am`, revert, and the apply backend of rebase are fairly
straightforward.  However, with the merge backend of rebase we have to
be more careful, since it powers interactive rebases and
  - the interactive machinery internally uses `git commit --amend` for
    `squash` and `reword` directives
  - users are expected to `git commit --amend` after hitting an `edit`
    or `break` directive
So, we need to be careful with rebase to only reject amending when doing
conflict resolution.

A few files under the rebase-merge/ directory provide us the necessary
information:

  - stopped-sha is written only when the rebase stops and hands control
    back to the user, so its presence marks a genuine stop -- as opposed
    to the sequencer's own internal `git commit --amend` while applying
    a squash, fixup, or reword, during which no stopped-sha exists.

  - amend is written only when the rebase stops with HEAD already
    pointing at the commit the user is meant to amend: a clean `edit`,
    or a fast-forward `reword`.  Its absence at a stop therefore means
    the commit did not apply, so HEAD is the previously-applied commit
    rather than the one being rebased -- exactly the case we refuse.

So for the merge backend we die when stopped-sha exists and amend does
not.  This covers a plain conflicted pick as well as a conflicted `edit`
(both leave HEAD on the previously-applied commit), while still allowing
a clean `edit` or `reword` stop and a `break` stop (no stopped-sha).
stopped-sha is unlinked at the start of the resume loop, so a resumed
squash's internal amend is unaffected.

Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren <newren@gmail.com> · 2026-09-01 22:24 UTC · committed by Junio C Hamano

Browse files
6 files changed, +210 −6
modified builtin/commit.c
@@ -1324,15 +1324,30 @@ static int parse_and_validate_options(int argc, const char *argv[],
1324 1324 use_editor = 0;
1325 1325
1326 1326 /* Sanity check options */
1327 - if (amend && !current_head)
1328 - die(_("You have nothing to amend."));
1329 - if (amend && whence != FROM_COMMIT) {
1330 - if (whence == FROM_MERGE)
1327 + if (amend) {
1328 + if (!current_head)
1329 + die(_("You have nothing to amend."));
1330 + /*
1331 + * Refuse to amend in the middle of any operation that is
1332 + * meant to record its result as a new commit on top of HEAD
1333 + * rather than by rewriting HEAD.
1334 + */
1335 + switch (sequencer_ongoing_operation(s->repo, whence)) {
1336 + case ONGOING_NONE:
1337 + break;
1338 + case ONGOING_MERGE:
1331 1339 die(_("You are in the middle of a merge -- cannot amend."));
1332 - else if (is_from_cherry_pick(whence))
1340 + case ONGOING_CHERRY_PICK:
1333 1341 die(_("You are in the middle of a cherry-pick -- cannot amend."));
1334 - else if (is_from_rebase_now_empty(whence))
1342 + case ONGOING_REBASE_NOW_EMPTY:
1335 1343 die(_("The now-empty commit has been dropped -- cannot amend."));
1344 + case ONGOING_REVERT:
1345 + die(_("You are in the middle of a revert -- cannot amend."));
1346 + case ONGOING_AM:
1347 + die(_("You are in the middle of an am session -- cannot amend."));
1348 + case ONGOING_REBASE_CONFLICT:
1349 + die(_("You are resolving conflicts during a rebase -- cannot amend."));
1350 + }
1336 1351 }
1337 1352 if (fixup_message && squash_message)
1338 1353 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup");
modified sequencer.c
@@ -142,6 +142,13 @@ static GIT_PATH_FUNC(rebase_path_author_script, "rebase-merge/author-script")
142 142 * command is processed, this file is deleted.
143 143 */
144 144 static GIT_PATH_FUNC(rebase_path_amend, "rebase-merge/amend")
145 + /*
146 + * The apply ("am") backend keeps its state in the rebase-apply directory;
147 + * the "applying" file within it marks a plain `git am` (as opposed to an
148 + * apply-based rebase).
149 + */
150 + static GIT_PATH_FUNC(apply_dir, "rebase-apply")
151 + static GIT_PATH_FUNC(apply_path_applying, "rebase-apply/applying")
145 152 /*
146 153 * When we stop at a given patch via the "edit" command, this file contains
147 154 * the commit object name of the corresponding patch.
@@ -6865,6 +6872,56 @@ int sequencer_determine_whence(struct repository *r, enum commit_whence *whence)
6865 6872 return 0;
6866 6873 }
6867 6874
6875 + enum ongoing_operation sequencer_ongoing_operation(struct repository *r,
6876 + enum commit_whence whence)
6877 + {
6878 + /*
6879 + * The merge, cherry-pick, and (empty) rebase-pick stops are already
6880 + * distinguished by 'whence'.
6881 + */
6882 + switch (whence) {
6883 + case FROM_MERGE:
6884 + return ONGOING_MERGE;
6885 + case FROM_CHERRY_PICK_SINGLE:
6886 + case FROM_CHERRY_PICK_MULTI:
6887 + return ONGOING_CHERRY_PICK;
6888 + case FROM_REBASE_NOW_EMPTY:
6889 + return ONGOING_REBASE_NOW_EMPTY;
6890 + case FROM_COMMIT:
6891 + break;
6892 + }
6893 +
6894 + /*
6895 + * 'whence' is FROM_COMMIT, but we may still be in the middle of an
6896 + * operation that records its result on top of HEAD; detect those
6897 + * from their on-disk state.
6898 + */
6899 +
6900 + /* In the middle of a revert? */
6901 + if (refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD"))
6902 + return ONGOING_REVERT;
6903 +
6904 + /* In the middle of an `am`? */
6905 + if (file_exists(apply_path_applying()))
6906 + return ONGOING_AM;
6907 +
6908 + /*
6909 + * In the middle of a rebase that stopped for conflict resolution?
6910 + * The apply backend only ever stops for conflicts, so the presence
6911 + * of its state directory is enough. The merge backend writes
6912 + * stopped-sha whenever it hands control back to the user, but omits
6913 + * `amend` unless it stopped with HEAD already pointing at the commit
6914 + * to be amended (a clean edit/reword stop); its absence therefore
6915 + * marks a conflicted stop.
6916 + */
6917 + if (file_exists(apply_dir()) ||
6918 + (file_exists(rebase_path_stopped_sha()) &&
6919 + !file_exists(rebase_path_amend())))
6920 + return ONGOING_REBASE_CONFLICT;
6921 +
6922 + return ONGOING_NONE;
6923 + }
6924 +
6868 6925 int sequencer_get_update_refs_state(const char *wt_dir,
6869 6926 struct string_list *refs)
6870 6927 {
modified sequencer.h
@@ -269,6 +269,29 @@ int sequencer_get_last_command(struct repository* r,
269 269 enum replay_action *action);
270 270 int sequencer_determine_whence(struct repository *r, enum commit_whence *whence);
271 271
272 + /*
273 + * An in-progress operation that records its result (often a conflict
274 + * resolution) as a new commit on top of HEAD, during which amending
275 + * HEAD via "git commit --amend" is almost always a mistake.
276 + */
277 + enum ongoing_operation {
278 + ONGOING_NONE = 0,
279 + ONGOING_MERGE,
280 + ONGOING_CHERRY_PICK,
281 + ONGOING_REBASE_NOW_EMPTY,
282 + ONGOING_REVERT,
283 + ONGOING_AM,
284 + ONGOING_REBASE_CONFLICT
285 + };
286 +
287 + /*
288 + * Return which in-progress operation, if any, is underway; see enum
289 + * ongoing_operation. 'whence' is the origin already computed for the
290 + * pending commit.
291 + */
292 + enum ongoing_operation sequencer_ongoing_operation(struct repository *r,
293 + enum commit_whence whence);
294 +
272 295 /**
273 296 * Append the set of ref-OID pairs that are currently stored for the 'git
274 297 * rebase --update-refs' feature if such a rebase is currently happening.
modified t/t3404-rebase-interactive.sh
@@ -1854,6 +1854,93 @@ test_expect_success 'correct error message for commit --amend after empty pick'
1854 1854 test_grep "now-empty commit has been dropped -- cannot amend." err
1855 1855 '
1856 1856
1857 + test_expect_success 'commit --amend is refused at a rebase conflict stop' '
1858 + test_when_finished "git rebase --abort" &&
1859 + git checkout --detach conflict-branch &&
1860 + (
1861 + set_fake_editor &&
1862 + FAKE_LINES="1 3" &&
1863 + export FAKE_LINES &&
1864 + test_must_fail git rebase -i A
1865 + ) &&
1866 + test_path_is_file .git/rebase-merge/patch &&
1867 + test_path_is_missing .git/rebase-merge/amend &&
1868 + echo resolved >conflict &&
1869 + git add conflict &&
1870 + test_must_fail git commit --amend --no-edit 2>err &&
1871 + test_grep "You are resolving conflicts during a rebase -- cannot amend" err
1872 + '
1873 +
1874 + test_expect_success 'commit --amend is refused when an "edit" pick conflicts' '
1875 + test_when_finished "git rebase --abort" &&
1876 + git checkout --detach conflict-branch &&
1877 + (
1878 + set_fake_editor &&
1879 + FAKE_LINES="1 edit 3" &&
1880 + export FAKE_LINES &&
1881 + test_must_fail git rebase -i A
1882 + ) &&
1883 + test_path_is_file .git/rebase-merge/patch &&
1884 + test_path_is_missing .git/rebase-merge/amend &&
1885 + echo resolved >conflict &&
1886 + git add conflict &&
1887 + test_must_fail git commit --amend --no-edit 2>err &&
1888 + test_grep "You are resolving conflicts during a rebase -- cannot amend" err
1889 + '
1890 +
1891 + test_expect_success 'commit --amend is allowed at a rebase edit stop' '
1892 + test_when_finished "git rebase --abort" &&
1893 + git checkout --detach no-conflict-branch &&
1894 + (
1895 + set_fake_editor &&
1896 + FAKE_LINES="edit 1 2 3 4" &&
1897 + export FAKE_LINES &&
1898 + git rebase -i A
1899 + ) &&
1900 + test_path_is_file .git/rebase-merge/amend &&
1901 + echo tweak >fileJ &&
1902 + git add fileJ &&
1903 + git commit --amend --no-edit
1904 + '
1905 +
1906 + test_expect_success 'commit --amend is allowed at a rebase break stop' '
1907 + test_when_finished "git rebase --abort" &&
1908 + git checkout --detach no-conflict-branch &&
1909 + (
1910 + set_fake_editor &&
1911 + FAKE_LINES="break 1 2 3 4" &&
1912 + export FAKE_LINES &&
1913 + git rebase -i A
1914 + ) &&
1915 + test_must_fail git rev-parse --verify REBASE_HEAD &&
1916 + echo tweak >fileJ &&
1917 + git add fileJ &&
1918 + git commit --amend --no-edit
1919 + '
1920 +
1921 + test_expect_success 'commit --amend is refused at an apply-backend conflict stop' '
1922 + test_when_finished "rm -rf apply-backend" &&
1923 + test_create_repo apply-backend &&
1924 + (
1925 + cd apply-backend &&
1926 + test_commit base file &&
1927 + git branch -M mainline &&
1928 + test_commit upstream file upstream &&
1929 + git checkout -b side mainline~1 &&
1930 + test_commit conflicting file side &&
1931 + test_commit unrelated other &&
1932 + test_must_fail git rebase --apply mainline &&
1933 + # the apply backend only ever stops for conflicts, and
1934 + # leaves HEAD on the previously-applied commit
1935 + test_path_is_dir .git/rebase-apply &&
1936 + test_path_is_missing .git/rebase-apply/applying &&
1937 + echo resolved >file &&
1938 + git add file &&
1939 + test_must_fail git commit --amend --no-edit 2>err &&
1940 + test_grep "You are resolving conflicts during a rebase -- cannot amend" err
1941 + )
1942 + '
1943 +
1857 1944 test_expect_success 'todo has correct onto hash' '
1858 1945 GIT_SEQUENCE_EDITOR=cat git rebase -i no-conflict-branch~4 no-conflict-branch >actual &&
1859 1946 onto=$(git rev-parse --short HEAD~4) &&
modified t/t3507-cherry-pick-conflict.sh
@@ -364,6 +364,17 @@ test_expect_success 'failed revert sets REVERT_HEAD' '
364 364 test_cmp_rev picked REVERT_HEAD
365 365 '
366 366
367 + test_expect_success 'commit --amend of revert fails' '
368 + pristine_detach initial &&
369 +
370 + test_must_fail git revert picked &&
371 + echo resolved >foo &&
372 + git add foo &&
373 + test_must_fail git commit --amend 2>err &&
374 +
375 + test_grep "in the middle of a revert -- cannot amend." err
376 + '
377 +
367 378 test_expect_success 'successful revert does not set REVERT_HEAD' '
368 379 pristine_detach base &&
369 380 git revert base &&
modified t/t4151-am-abort.sh
@@ -63,6 +63,17 @@ do
63 63
64 64 done
65 65
66 + test_expect_success 'commit --amend during a failed am fails' '
67 + git reset --hard initial &&
68 + cp file-2-expect file-2 &&
69 + test_must_fail git am 000[1245]-*.patch &&
70 + echo resolved >file-1 &&
71 + git add file-1 &&
72 + test_must_fail git commit --amend 2>err &&
73 + test_grep "in the middle of an am session -- cannot amend." err &&
74 + git am --abort
75 + '
76 +
66 77 test_expect_success 'am -3 --skip removes otherfile-4' '
67 78 git reset --hard initial &&
68 79 test_must_fail git am -3 0003-*.patch &&