File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -64,3 +64,17 @@ void NORETURN die_resolve_conflict(const char *me)
6464 error_resolve_conflict (me );
6565 die ("Exiting because of an unresolved conflict." );
6666}
67+
68+ void detach_advice (const char * new_name )
69+ {
70+ const char fmt [] =
71+ "Note: checking out '%s'.\n\n"
72+ "You are in 'detached HEAD' state. You can look around, make experimental\n"
73+ "changes and commit them, and you can discard any commits you make in this\n"
74+ "state without impacting any branches by performing another checkout.\n\n"
75+ "If you want to create a new branch to retain commits you create, you may\n"
76+ "do so (now or later) by using -b with the checkout command again. Example:\n\n"
77+ " git checkout -b new_branch_name\n\n" ;
78+
79+ fprintf (stderr , fmt , new_name );
80+ }
Original file line number Diff line number Diff line change @@ -14,5 +14,6 @@ int git_default_advice_config(const char *var, const char *value);
1414void advise (const char * advice , ...);
1515int error_resolve_conflict (const char * me );
1616extern void NORETURN die_resolve_conflict (const char * me );
17+ void detach_advice (const char * new_name );
1718
1819#endif /* ADVICE_H */
Original file line number Diff line number Diff line change @@ -514,20 +514,6 @@ static void report_tracking(struct branch_info *new)
514514 strbuf_release (& sb );
515515}
516516
517- static void detach_advice (const char * old_path , const char * new_name )
518- {
519- const char fmt [] =
520- "Note: checking out '%s'.\n\n"
521- "You are in 'detached HEAD' state. You can look around, make experimental\n"
522- "changes and commit them, and you can discard any commits you make in this\n"
523- "state without impacting any branches by performing another checkout.\n\n"
524- "If you want to create a new branch to retain commits you create, you may\n"
525- "do so (now or later) by using -b with the checkout command again. Example:\n\n"
526- " git checkout -b new_branch_name\n\n" ;
527-
528- fprintf (stderr , fmt , new_name );
529- }
530-
531517static void update_refs_for_switch (struct checkout_opts * opts ,
532518 struct branch_info * old ,
533519 struct branch_info * new )
@@ -575,7 +561,7 @@ static void update_refs_for_switch(struct checkout_opts *opts,
575561 REF_NODEREF , DIE_ON_ERR );
576562 if (!opts -> quiet ) {
577563 if (old -> path && advice_detached_head )
578- detach_advice (old -> path , new -> name );
564+ detach_advice (new -> name );
579565 describe_detached_head (_ ("HEAD is now at" ), new -> commit );
580566 }
581567 } else if (new -> path ) { /* Switch branches. */
Original file line number Diff line number Diff line change @@ -563,7 +563,10 @@ static int checkout(void)
563563 "unable to checkout.\n" ));
564564 return 0 ;
565565 }
566- if (strcmp (head , "HEAD" )) {
566+ if (!strcmp (head , "HEAD" )) {
567+ if (advice_detached_head )
568+ detach_advice (sha1_to_hex (sha1 ));
569+ } else {
567570 if (prefixcmp (head , "refs/heads/" ))
568571 die (_ ("HEAD not found below refs/heads!" ));
569572 }
You can’t perform that action at this time.
0 commit comments