Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
unpack-trees: suggest using 'git stash' when checkout fails
When a branch switch fails due to local changes and
new users who are not familiar with the error message often
get confused about how to move ahead and resolve the issue as
the previous error message only suggests to commit or stash the changes
but doesn't explain how to do that or what the next steps are.

This patch enhances the error message with more specific
instructions in a concise manner to help users understand
how to resolve the issue and move their local changes
safely to the other branch using stash.

Signed-off-by: Arsh Srivastava <arshsrivastava00@gmail.com>
  • Loading branch information
Arsh123344423 committed Mar 12, 2026
commit 72cb5506214aef4aa4ae12fb3ca8e95ebc841a6e
6 changes: 4 additions & 2 deletions t/t6439-merge-co-error-msgs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ cat >expect <<\EOF
error: Your local changes to the following files would be overwritten by checkout:
rep/one
rep/two
Please commit your changes or stash them before you switch branches.
To move you local changes safely to the other branch,
Please try 'git stash' followed by 'git checkout <branch>' followed by 'git stash pop' for safe merge.
Aborting
EOF

Expand All @@ -98,7 +99,8 @@ cat >expect <<\EOF
error: Your local changes to the following files would be overwritten by checkout:
rep/one
rep/two
Please commit your changes or stash them before you switch branches.
To move you local changes safely to the other branch,
Please try 'git stash' followed by 'git checkout <branch>' followed by 'git stash pop' for safe merge.
Aborting
EOF

Expand Down
3 changes: 2 additions & 1 deletion t/t7406-submodule-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ test_expect_success 'submodule update should fail due to local changes' '
sed "s/^> //" >expect <<-\EOF &&
> 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.
> To move you local changes safely to the other branch,
> Please try '\''git stash'\'' followed by '\''git checkout <branch>'\'' followed by '\''git stash pop'\'' for safe merge.
> Aborting
> fatal: Unable to checkout OID in submodule path '\''submodule'\''
EOF
Expand Down
7 changes: 5 additions & 2 deletions unpack-trees.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,11 @@ 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");
"To move you local changes safely to the other branch,\n"
"Please try 'git stash' followed by 'git checkout <branch>' followed by 'git stash pop' for safe 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.");
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"
Expand Down
Loading