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

[PATCH v5 2/2] checkout: separate autostash conflict advice from branch-switch message

From
Harald Nordgren via GitGitGadget <gitgitgadget@gmail.com>
Date
Sep 3, 2026, 14:39 UTC
Message-ID
<d18ff3ea9a018b6ae207bd4f3c7740b96b3197c0.1788446398.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.2364.v5.git.git.1788446398.gitgitgadget@gmail.com>
From: Harald Nordgren <haraldnordgren@gmail.com>

"git checkout -m" stashes the user's local changes when it cannot perform the checkout, and then applies the stash. When applying the stash results in conflicts, the advice on how to deal with them is printed directly on top of the branch-switch message ("Switched to branch ..."), making the two hard to tell apart. Print a blank line in between so that the advice and the branch-switch message are visually distinct.

apply_autostash_ref() reports whether applying the stash resulted in conflicts via its enum stash_apply_result return value, so only print the blank line in the conflicted case.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 builtin/checkout.c | 15 +++++++++------
 t/t7201-co.sh      | 16 ++++++++++++----
 2 files changed, 21 insertions(+), 10 deletions(-)
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 55e3a89a85..c960f98287 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1166,6 +1166,7 @@ static int switch_branches(const struct checkout_opts *opts,
 	int flag, writeout_error = 0;
 	int do_merge = 1;
 	int created_autostash = 0;
+	enum stash_apply_result autostash_res = STASH_APPLY_CLEAN;
 	struct strbuf old_commit_shortname = STRBUF_INIT;
 	struct strbuf autostash_msg = STRBUF_INIT;
 	const char *stash_label_base = NULL;
@@ -1237,12 +1238,12 @@ static int switch_branches(const struct checkout_opts *opts,
 				git_config_push_parameter(cfg.buf);
 				strbuf_release(&cfg);
 			}
-			apply_autostash_ref(the_repository,
-					    "CHECKOUT_AUTOSTASH_HEAD",
-					    new_branch_info->name,
-					    "local",
-					    stash_label_base,
-					    autostash_msg.buf);
+			autostash_res = apply_autostash_ref(the_repository,
+				    "CHECKOUT_AUTOSTASH_HEAD",
+				    new_branch_info->name,
+				    "local",
+				    stash_label_base,
+				    autostash_msg.buf);
 		}
 		if (ret) {
 			branch_info_release(&old_branch_info);
@@ -1255,6 +1256,8 @@ static int switch_branches(const struct checkout_opts *opts,
 	if (!opts->quiet && !old_branch_info.path && old_branch_info.commit && new_branch_info->commit != old_branch_info.commit)
 		orphaned_commit_warning(old_branch_info.commit, new_branch_info->commit);
 
+	if (autostash_res == STASH_APPLY_CONFLICT && !opts->quiet)
+		fputc('\n', stderr);
 	update_refs_for_switch(opts, &old_branch_info, new_branch_info);
 
 	if (created_autostash) {
diff --git a/t/t7201-co.sh b/t/t7201-co.sh
index 0ddd1ad7aa..9ea9462914 100755
--- a/t/t7201-co.sh
+++ b/t/t7201-co.sh
@@ -236,10 +236,18 @@ test_expect_success 'checkout -m creates a recoverable stash on conflict' '
 	test_must_fail git checkout side 2>stderr &&
 	test_grep "Your local changes" stderr &&
 	git checkout -m side >actual 2>&1 &&
-	test_grep "resulted in conflicts" actual &&
-	test_grep "git stash drop" actual &&
-	test_grep "git stash pop" actual &&
-	test_grep "The following paths have local changes" actual &&
+	cat >expect <<-EOF &&
+	Your local changes are stashed, however applying them
+	resulted in conflicts.  You can either resolve the conflicts
+	and then discard the stash with "git stash drop", or, if you
+	do not want to resolve them now, run "git reset --hard" and
+	apply the local changes later by running "git stash pop".
+
+	Switched to branch ${SQ}side${SQ}
+	The following paths have local changes:
+	M	one
+	EOF
+	test_cmp expect actual &&
 	git log -p -1 --format="%gs%n%B" -g --diff-merges=1 refs/stash >actual &&
 	sed /^index/d actual >actual.trimmed &&
 	cat >expect <<-EOF &&
-- 
gitgitgadget
Previous: Harald Nordgren via GitGitGadgetNext: Junio C Hamano
Message 36 of 37 in “checkout -m: refine autostash fallback”
  1. 0/2 checkout -m: refine autostash fallbackHarald Nordgren via GitGitGadget, Jul 25, 2026
  2. 1/2 sequencer: teach autostash apply to report conflictsHarald Nordgren via GitGitGadget, Jul 25, 2026
  3. Phillip WoodAug 27, 2026
  4. Harald NordgrenAug 31, 2026
  5. phillip.wood123@gmail.comAug 31, 2026
  6. 2/2 checkout -m: refine autostash fallbackHarald Nordgren via GitGitGadget, Jul 25, 2026
  7. Junio C HamanoJul 28, 2026
  8. Phillip WoodAug 27, 2026
  9. Junio C HamanoAug 26, 2026
  10. Phillip WoodAug 27, 2026
  11. checkout: print blank line after autostash conflict adviceHarald Nordgren via GitGitGadget, Aug 31, 2026
  12. Junio C HamanoAug 31, 2026
  13. Phillip WoodSep 1, 2026
  14. Junio C HamanoSep 1, 2026
  15. checkout: separate autostash conflict advice from branch-switch messageHarald Nordgren via GitGitGadget, Sep 1, 2026
  16. Phillip WoodSep 1, 2026
  17. Junio C HamanoSep 1, 2026
  18. 0/2 checkout -m: refine autostash fallbackHarald Nordgren via GitGitGadget, Sep 2, 2026
  19. 1/2 stash: reserve exit status 1 for conflictsHarald Nordgren via GitGitGadget, Sep 2, 2026
  20. Junio C HamanoSep 2, 2026
  21. Junio C HamanoSep 2, 2026
  22. Phillip WoodSep 3, 2026
  23. Harald NordgrenSep 3, 2026
  24. Junio C HamanoSep 3, 2026
  25. Harald NordgrenSep 3, 2026
  26. Junio C HamanoSep 3, 2026
  27. Harald NordgrenSep 4, 2026
  28. Phillip WoodSep 4, 2026
  29. Harald NordgrenSep 4, 2026
  30. Junio C HamanoSep 4, 2026
  31. 2/2 checkout: separate autostash conflict advice from branch-switch messageHarald Nordgren via GitGitGadget, Sep 2, 2026
  32. Junio C HamanoSep 2, 2026
  33. Phillip WoodSep 3, 2026
  34. 0/2 checkout -m: refine autostash fallbackHarald Nordgren via GitGitGadget, Sep 3, 2026
  35. 1/2 stash: reserve exit status 1 for conflictsHarald Nordgren via GitGitGadget, Sep 3, 2026
  36. 2/2 checkout: separate autostash conflict advice from branch-switch messageHarald Nordgren via GitGitGadget, Sep 3, 2026
  37. Junio C HamanoSep 3, 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.