Skip to content

Commit f34c202

Browse files
committed
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>
1 parent d181b93 commit f34c202

3 files changed

Lines changed: 11 additions & 5 deletions

File tree

t/t6439-merge-co-error-msgs.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ cat >expect <<\EOF
7676
error: Your local changes to the following files would be overwritten by checkout:
7777
rep/one
7878
rep/two
79-
Please commit your changes or stash them before you switch branches.
79+
To move you local changes safely to the other branch,
80+
Please try 'git stash' followed by 'git checkout <branch>' followed by 'git stash pop' for safe merge.
8081
Aborting
8182
EOF
8283

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

t/t7406-submodule-update.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ test_expect_success 'submodule update should fail due to local changes' '
211211
sed "s/^> //" >expect <<-\EOF &&
212212
> error: Your local changes to the following files would be overwritten by checkout:
213213
> file
214-
> Please commit your changes or stash them before you switch branches.
214+
> To move you local changes safely to the other branch,
215+
> Please try '\''git stash'\'' followed by '\''git checkout <branch>'\'' followed by '\''git stash pop'\'' for safe merge.
215216
> Aborting
216217
> fatal: Unable to checkout OID in submodule path '\''submodule'\''
217218
EOF

unpack-trees.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,11 @@ void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,
128128
if (!strcmp(cmd, "checkout"))
129129
msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
130130
? _("Your local changes to the following files would be overwritten by checkout:\n%%s"
131-
"Please commit your changes or stash them before you switch branches.")
132-
: _("Your local changes to the following files would be overwritten by checkout:\n%%s");
131+
"To move you local changes safely to the other branch,\n"
132+
"Please try 'git stash' followed by 'git checkout <branch>' followed by 'git stash pop'."
133+
)
134+
: _("Your local changes to the following files would be overwritten by checkout:\n%%s"
135+
"Please commit your changes or stash them before you switch branches.");
133136
else if (!strcmp(cmd, "merge"))
134137
msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
135138
? _("Your local changes to the following files would be overwritten by merge:\n%%s"

0 commit comments

Comments
 (0)