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

[PATCH v6] unpack-trees: suggesting 'git checkout -m' with its repercussions

From
Arsh Srivastava via GitGitGadget <gitgitgadget@gmail.com>
Date
Mar 12, 2026, 04:00 UTC
Message-ID
<pull.2233.v6.git.git.1773288013936.gitgitgadget@gmail.com>
In-Reply-To
<pull.2233.v5.git.git.1773251369.gitgitgadget@gmail.com>
From: Arsh Srivastava <arshsrivastava00@gmail.com>

This comment is an extention to the already existing stash comment. Added updated comment over the already existing function "setup_unpack_trees_porcelain" with "git checkout -m" and its repercussions I have also mentioned the repercussions of using "-m".

Signed-off-by: Arsh Srivastava arshsrivastava00@gmail.com
---
    Advice on checkout dirty files
    
    This is my submission for microproject [GSOC]
    
    This patch extends the current message to help users better understand
    risks and alternatives to stashing their changes.
    
    The alternative consists of
    
    > git checkout -m and its repercussions.
    
    This extended message is tied to the existing advice.commitBeforeMerge
    configuration and can be silenced with:
    
    > git config set advice.commitBeforeMerge false
    
    Changes:
    
    > unpack-trees.c: expanded the already existing message.
    > t/t6439-merge-co-error-msgs.sh: update expected output for merge and
    > checkout tests. t/t7406-submodule-update.sh: update expected output
    > for submodule checkout tests.
    
    Signed-off-by: Arsh Srivastava arshsrivastava00@gmail.com
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2233%2FArsh123344423%2Fadvice_on_checkout_dirty_files-v6
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2233/Arsh123344423/advice_on_checkout_dirty_files-v6
Pull-Request: https://github.com/git/git/pull/2233
Range-diff vs v5:
 1:  eb5639dbc3 < -:  ---------- advice: add stashBeforeCheckout advice for dirty branch switches
 2:  e88c851701 < -:  ---------- advice: add stashBeforeCheckout advice for dirty branch switches [GSOC]
 3:  1cc22f437e ! 1:  e0ba5fad82 unpack-trees: suggesting 'git checkout -m <branch>' with its repercussions
     @@ Metadata
      Author: Arsh Srivastava <arshsrivastava00@gmail.com>
      
       ## Commit message ##
     -    unpack-trees: suggesting 'git checkout -m <branch>' with its repercussions
     +    unpack-trees: suggesting 'git checkout -m' with its repercussions
      
          This comment is an extention to the already existing stash comment.
     -
     -    After the valuable insights given to me by git maintaniers.
     -    I removed the new function I introduced.
          Added updated comment over the already existing function
     -    "setup_unpack_trees_porcelain" with "git checkout -m <branch>"
     +    "setup_unpack_trees_porcelain" with "git checkout -m"
          and its repercussions
          I have also mentioned the repercussions of using "-m".
      
     -    Signed-off-by: Arsh Srivastava <arshsrivastava00@gmail.com>
     -
     - ## Documentation/config/advice.adoc ##
     -@@ Documentation/config/advice.adoc: all advice messages.
     - 		Shown when a sparse index is expanded to a full index, which is likely
     - 		due to an unexpected set of files existing outside of the
     - 		sparse-checkout.
     --	stashBeforeCheckout::
     --		Shown when the user attempts to switch branches but has
     --		local modifications that would be overwritten by the
     --		operation, to suggest using linkgit:git-stash[1] to
     --		save changes before switching.
     - 	statusAheadBehind::
     - 		Shown when linkgit:git-status[1] computes the ahead/behind
     - 		counts for a local ref compared to its remote tracking ref,
     -
     - ## advice.c ##
     -@@ advice.c: static struct {
     - 	[ADVICE_SET_UPSTREAM_FAILURE]			= { "setUpstreamFailure" },
     - 	[ADVICE_SKIPPED_CHERRY_PICKS]			= { "skippedCherryPicks" },
     - 	[ADVICE_SPARSE_INDEX_EXPANDED]			= { "sparseIndexExpanded" },
     --	[ADVICE_STASH_BEFORE_CHECKOUT] = { "stashBeforeCheckout" },
     - 	[ADVICE_STATUS_AHEAD_BEHIND_WARNING]		= { "statusAheadBehindWarning" },
     - 	[ADVICE_STATUS_HINTS]				= { "statusHints" },
     - 	[ADVICE_STATUS_U_OPTION]			= { "statusUoption" },
     -@@ advice.c: void advise_on_moving_dirty_path(struct string_list *pathspec_list)
     - 			    "* Use \"git add --sparse <paths>\" to update the index\n"
     - 			    "* Use \"git sparse-checkout reapply\" to apply the sparsity rules"));
     - }
     --
     --void advise_on_checkout_dirty_files(struct string_list *file_list)
     --{
     --    struct string_list_item *item;
     --
     --    if (!file_list->nr)
     --	return;
     --
     --    fprintf(stderr, _("The following files have local modifications that would\n"
     --		      "be overwritten by switching branches:\n"));
     --    for_each_string_list_item(item, file_list)
     --	fprintf(stderr, "\t%s\n", item->string);
     --
     --    advise_if_enabled(ADVICE_STASH_BEFORE_CHECKOUT,
     --		      _("To carry your local changes over to the new branch, use:\n"
     --			"\n"
     --			"\tgit checkout -m <branch>\n"
     --			));
     --}
     -
     - ## advice.h ##
     -@@ advice.h: enum advice_type {
     - 	ADVICE_SET_UPSTREAM_FAILURE,
     - 	ADVICE_SKIPPED_CHERRY_PICKS,
     - 	ADVICE_SPARSE_INDEX_EXPANDED,
     --	ADVICE_STASH_BEFORE_CHECKOUT,
     - 	ADVICE_STATUS_AHEAD_BEHIND_WARNING,
     - 	ADVICE_STATUS_HINTS,
     - 	ADVICE_STATUS_U_OPTION,
     -@@ advice.h: void NORETURN die_ff_impossible(void);
     - void advise_on_updating_sparse_paths(struct string_list *pathspec_list);
     - void detach_advice(const char *new_name);
     - void advise_on_moving_dirty_path(struct string_list *pathspec_list);
     --void advise_on_checkout_dirty_files(struct string_list *file_list);
     - 
     - #endif /* ADVICE_H */
     +    Signed-off-by: Arsh Srivastava arshsrivastava00@gmail.com
      
       ## t/t6439-merge-co-error-msgs.sh ##
      @@ t/t6439-merge-co-error-msgs.sh: error: Your local changes to the following files would be overwritten by checkou
 t/t6439-merge-co-error-msgs.sh | 6 ++++++
 t/t7406-submodule-update.sh    | 3 +++
 unpack-trees.c                 | 9 +++++++--
 3 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/t/t6439-merge-co-error-msgs.sh b/t/t6439-merge-co-error-msgs.sh
index 643c9368e0..cf996e0e51 100755
--- a/t/t6439-merge-co-error-msgs.sh
+++ b/t/t6439-merge-co-error-msgs.sh
@@ -77,6 +77,9 @@ error: Your local changes to the following files would be overwritten by checkou
 	rep/one
 	rep/two
 Please commit your changes or stash them before you switch branches.
+Try using 'git checkout -m <branch>' for a quick fix.
+Please Note :- that using -m (merge) will not save your changes, rather would directly merge them.
+Meaning if you are not able to resolve conflicts and does --hard reset your local changes would be gone.
 Aborting
 EOF
 
@@ -99,6 +102,9 @@ error: Your local changes to the following files would be overwritten by checkou
 	rep/one
 	rep/two
 Please commit your changes or stash them before you switch branches.
+Try using 'git checkout -m <branch>' for a quick fix.
+Please Note :- that using -m (merge) will not save your changes, rather would directly merge them.
+Meaning if you are not able to resolve conflicts and does --hard reset your local changes would be gone.
 Aborting
 EOF
 
diff --git a/t/t7406-submodule-update.sh b/t/t7406-submodule-update.sh
index 3adab12091..2e4cc9b3fe 100755
--- a/t/t7406-submodule-update.sh
+++ b/t/t7406-submodule-update.sh
@@ -212,6 +212,9 @@ test_expect_success 'submodule update should fail due to local changes' '
 	> error: Your local changes to the following files would be overwritten by checkout:
 	> 	file
 	> Please commit your changes or stash them before you switch branches.
+	> Try using '\''git checkout -m <branch>'\'' for a quick fix.
+	> Please Note :- that using -m (merge) will not save your changes, rather would directly merge them.
+	> Meaning if you are not able to resolve conflicts and does --hard reset your local changes would be gone.
 	> Aborting
 	> fatal: Unable to checkout OID in submodule path '\''submodule'\''
 	EOF
diff --git a/unpack-trees.c b/unpack-trees.c
index 998a1e6dc7..9835a82455 100644
--- a/unpack-trees.c
+++ b/unpack-trees.c
@@ -128,8 +128,13 @@ void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,
 	if (!strcmp(cmd, "checkout"))
 		msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
 		      ? _("Your local changes to the following files would be overwritten by checkout:\n%%s"
-			  "Please commit your changes or stash them before you switch branches.")
-		      : _("Your local changes to the following files would be overwritten by checkout:\n%%s");
+			  "Please commit your changes or stash them before you switch branches.\n"
+			  "Try using 'git checkout -m <branch>' for a quick fix.\n"
+			  "Please Note :- that using -m (merge) will not save your changes, rather would directly merge them.\n"
+			  "Meaning if you are not able to resolve conflicts and does --hard reset your local changes would be gone."
+			  )
+		      : _("Your local changes to the following files would be overwritten by checkout:\n%%s"
+				"Please commit your changes or stash them before you switch branches.");
 	else if (!strcmp(cmd, "merge"))
 		msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
 		      ? _("Your local changes to the following files would be overwritten by merge:\n%%s"

base-commit: d181b9354cf85b44455ce3ca9e6af0b9559e0ae2
-- 
gitgitgadget
Previous: Arsh SrivastavaNext: Junio C Hamano
Message 37 of 62 in “Advice on checkout dirty files”
  1. 0/5 Advice on checkout dirty filesArsh Srivastava via GitGitGadget, Mar 10, 2026
  2. 1/5 diff: handle ANSI escape codes in prefix when calculating diffstat widthLorenzoPegorari via GitGitGadget, Mar 10, 2026
  3. 2/5 t4052: test for diffstat width when prefix contains ANSI escape codesLorenzoPegorari via GitGitGadget, Mar 10, 2026
  4. 3/5 repo: remove unnecessary variable shadowK Jayatheerth via GitGitGadget, Mar 10, 2026
  5. 4/5 The 13th batchJunio C Hamano via GitGitGadget, Mar 10, 2026
  6. 5/5 advice: add stashBeforeCheckout advice for dirty branch switchesArsh Srivastava via GitGitGadget, Mar 10, 2026
  7. Phillip WoodMar 10, 2026
  8. Arsh SrivastavaMar 10, 2026
  9. Arsh SrivastavaMar 10, 2026
  10. Junio C HamanoMar 10, 2026
  11. Arsh SrivastavaMar 10, 2026
  12. Junio C HamanoMar 10, 2026
  13. Arsh SrivastavaMar 10, 2026
  14. Arsh SrivastavaMar 10, 2026
  15. advice: add stashBeforeCheckout advice for dirty branch switchesArsh Srivastava via GitGitGadget, Mar 10, 2026
  16. Patrick SteinhardtMar 10, 2026
  17. Arsh SrivastavaMar 10, 2026
  18. Patrick SteinhardtMar 10, 2026
  19. 0/2 Advice on checkout dirty filesArsh Srivastava via GitGitGadget, Mar 10, 2026
  20. 1/2 advice: add stashBeforeCheckout advice for dirty branch switchesArsh Srivastava via GitGitGadget, Mar 10, 2026
  21. Arsh SrivastavaMar 10, 2026
  22. 2/2 advice: add stashBeforeCheckout advice for dirty branch switches [GSOC]Arsh Srivastava via GitGitGadget, Mar 10, 2026
  23. 0/5 Advice on checkout dirty filesArsh Srivastava via GitGitGadget, Mar 11, 2026
  24. 1/5 advice: add stashBeforeCheckout advice for dirty branch switchesArsh Srivastava via GitGitGadget, Mar 11, 2026
  25. 2/5 advice: add stashBeforeCheckout advice for dirty branch switches [GSOC]Arsh Srivastava via GitGitGadget, Mar 11, 2026
  26. 3/5 unpack-trees: suggesting 'git checkout -m <branch>' with its repercussionsArsh Srivastava via GitGitGadget, Mar 11, 2026
  27. 4/5 Updating tests and unpack-tress.c [GSOC]Arsh Srivastava via GitGitGadget, Mar 11, 2026
  28. 5/5 File updation [GSOC]Arsh Srivastava via GitGitGadget, Mar 11, 2026
  29. Junio C HamanoMar 11, 2026
  30. Arsh SrivastavaMar 11, 2026
  31. 0/3 Advice on checkout dirty filesArsh Srivastava via GitGitGadget, Mar 11, 2026
  32. 1/3 advice: add stashBeforeCheckout advice for dirty branch switchesArsh Srivastava via GitGitGadget, Mar 11, 2026
  33. 2/3 advice: add stashBeforeCheckout advice for dirty branch switches [GSOC]Arsh Srivastava via GitGitGadget, Mar 11, 2026
  34. 3/3 unpack-trees: suggesting 'git checkout -m <branch>' with its repercussionsArsh Srivastava via GitGitGadget, Mar 11, 2026
  35. Junio C HamanoMar 12, 2026
  36. Arsh SrivastavaMar 12, 2026
  37. unpack-trees: suggesting 'git checkout -m' with its repercussionsArsh Srivastava via GitGitGadget, Mar 12, 2026
  38. Junio C HamanoMar 12, 2026
  39. Arsh SrivastavaMar 12, 2026
  40. Junio C HamanoMar 12, 2026
  41. Arsh SrivastavaMar 12, 2026
  42. Junio C HamanoMar 12, 2026
  43. Arsh SrivastavaMar 12, 2026
  44. unpack-trees: suggest using 'git stash' when checkout failsArsh Srivastava via GitGitGadget, Mar 12, 2026
  45. Junio C HamanoMar 12, 2026
  46. Arsh SrivastavaMar 13, 2026
  47. Karthik NayakMar 13, 2026
  48. Arsh SrivastavaMar 13, 2026
  49. Junio C HamanoMar 13, 2026
  50. Arsh SrivastavaMar 13, 2026
  51. Arsh SrivastavaMar 13, 2026
  52. Karthik NayakMar 10, 2026
  53. Arsh SrivastavaMar 10, 2026
  54. Arsh SrivastavaMar 10, 2026
  55. Karthik NayakMar 10, 2026
  56. Arsh SrivastavaMar 10, 2026
  57. Arsh SrivastavaMar 10, 2026
  58. Junio C HamanoMar 10, 2026
  59. Karthik NayakMar 10, 2026
  60. Konstantin RyabitsevMar 14, 2026
  61. Arsh SrivastavaMar 10, 2026
  62. Arsh SrivastavaMar 10, 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.