git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] merge --abort: don't delete autostash before reset succeeds

From
KPKris Point <krispointcsgo@outlook.com>
Date
Jul 8, 2026, 01:51 UTC
Message-ID
<SI1PPF1BAF45F0FA46A6EED57B732BB04D7ABFF2@SI1PPF1BAF45F0F.apcprd02.prod.outlook.com>
From bf4b12438a83d81f2c8df6e39f6114ddd5002430 Mon Sep 17 00:00:00 2001
From: KrisPointCSGO <KrisPointCSGO@outlook.com>
Date: Tue, 7 Jul 2026 20:10:00 +0800
Subject: [PATCH] merge --abort: don't delete autostash before reset succeeds
To: git@vger.kernel.org
Cc: gitster@pobox.com

In cmd_merge()'s --abort path, MERGE_AUTOSTASH was deleted before cmd_reset() was called. If cmd_reset() failed (e.g. due to a locked index), the autostash was permanently lost.

Instead, read the MERGE_AUTOSTASH OID without deleting the ref, run cmd_reset() (which itself calls remove_branch_state() -> save_autostash_ref() to persist the stash), and only apply the autostash on success.

Reported-by: KrisPoint
Signed-off-by: KrisPoint <KrisPointCSGO@outlook.com>
---
 builtin/merge.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
diff --git a/builtin/merge.c b/builtin/merge.c
index 5b46a596f0..5d9a242027 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -1427,15 +1427,14 @@ int cmd_merge(int argc,
 		if (!file_exists(git_path_merge_head(the_repository)))
 			die(_("There is no merge to abort (MERGE_HEAD missing)."));
 
-		if (!refs_read_ref(get_main_ref_store(the_repository), "MERGE_AUTOSTASH", &stash_oid))
-			refs_delete_ref(get_main_ref_store(the_repository),
-					"", "MERGE_AUTOSTASH", &stash_oid,
-					REF_NO_DEREF);
+		refs_read_ref(get_main_ref_store(the_repository), "MERGE_AUTOSTASH", &stash_oid);
 
-		/* Invoke 'git reset --merge' */
+		/* Invoke 'git reset --merge' (which also cleans up merge state,
+		 * including saving the autostash to the stash list).
+		 */
 		ret = cmd_reset(nargc, nargv, prefix, the_repository);
 
-		if (!is_null_oid(&stash_oid)) {
+		if (!ret && !is_null_oid(&stash_oid)) {
 			oid_to_hex_r(stash_oid_hex, &stash_oid);
 			apply_autostash_oid(stash_oid_hex);
 		}
-- 
2.53.0

I've already changed the format to plain text. I don't think I did anything wrong.
Next: Phillip Wood
Message 1 of 3 in “merge --abort: don't delete autostash before reset succeeds”
  1. merge --abort: don't delete autostash before reset succeedsKris Point, Jul 8, 2026
  2. Phillip WoodJul 8, 2026
  3. Junio C HamanoJul 8, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.