Skip to content

Commit af9ded5

Browse files
pcloudsgitster
authored andcommitted
checkout: advice how to get out of detached HEAD mode
Detached HEAD mode is considered dangerous and confusing for newcomers and we print a big block of warning how to move forward. But we should also suggest the user the way to get out of it if they get into detached HEAD by mistake. While at there, I also suggest how to turn the advice off. This is another thing I find annoying with advices and should be dealt with in a more generic way. But that may require some refactoring in advice.c first. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent c2a8c1d commit af9ded5

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

advice.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,22 @@ void NORETURN die_conclude_merge(void)
191191
void detach_advice(const char *new_name)
192192
{
193193
const char *fmt =
194-
_("Note: checking out '%s'.\n\n"
194+
_("Note: checking out '%s'.\n"
195+
"\n"
195196
"You are in 'detached HEAD' state. You can look around, make experimental\n"
196197
"changes and commit them, and you can discard any commits you make in this\n"
197-
"state without impacting any branches by performing another checkout.\n\n"
198+
"state without impacting any branches by performing another checkout.\n"
199+
"\n"
198200
"If you want to create a new branch to retain commits you create, you may\n"
199-
"do so (now or later) by using -b with the checkout command again. Example:\n\n"
200-
" git checkout -b <new-branch-name>\n\n");
201+
"do so (now or later) by using -b with the checkout command again. Example:\n"
202+
"\n"
203+
" git checkout -b <new-branch-name>\n"
204+
"\n"
205+
"Or undo this checkout with:\n"
206+
"\n"
207+
" git checkout -\n"
208+
"\n"
209+
"Turn off this advice by setting config variable advice.detachedHead to false\n\n");
201210

202211
fprintf(stderr, fmt, new_name);
203212
}

t/t2020-checkout-detach.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ test_expect_success 'describe_detached_head prints no SHA-1 ellipsis when not as
206206
207207
git checkout -b <new-branch-name>
208208
209+
Or undo this checkout with:
210+
211+
git checkout -
212+
213+
Turn off this advice by setting config variable advice.detachedHead to false
214+
209215
HEAD is now at \$commit three
210216
EOF
211217
@@ -282,6 +288,12 @@ test_expect_success 'describe_detached_head does print SHA-1 ellipsis when asked
282288
283289
git checkout -b <new-branch-name>
284290
291+
Or undo this checkout with:
292+
293+
git checkout -
294+
295+
Turn off this advice by setting config variable advice.detachedHead to false
296+
285297
HEAD is now at \$commit... three
286298
EOF
287299

0 commit comments

Comments
 (0)