Skip to content

Commit ec8460b

Browse files
moygitster
authored andcommitted
push: better error messages when push.default = tracking
A common scenario is to create a new branch and push it (checkout -b && push [--set-upstream]). In this case, the user was getting "The current branch %s has no upstream branch.", which doesn't help much. Provide the user a command to push the current branch. To avoid the situation in the future, suggest --set-upstream. While we're there, also improve the error message in the "detached HEAD" case. We mention explicitly "detached HEAD" since this is the keyword to look for in documentations. Signed-off-by: Matthieu Moy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8978166 commit ec8460b

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

builtin/push.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,24 @@ static void set_refspecs(const char **refs, int nr)
6464
}
6565
}
6666

67-
static void setup_push_upstream(void)
67+
static void setup_push_upstream(struct remote *remote)
6868
{
6969
struct strbuf refspec = STRBUF_INIT;
7070
struct branch *branch = branch_get(NULL);
7171
if (!branch)
72-
die("You are not currently on a branch.");
72+
die("You are not currently on a branch.\n"
73+
"To push the history leading to the current (detached HEAD)\n"
74+
"state now, use\n"
75+
"\n"
76+
" git push %s HEAD:<name-of-remote-branch>\n",
77+
remote->name);
7378
if (!branch->merge_nr || !branch->merge)
74-
die("The current branch %s has no upstream branch.",
79+
die("The current branch %s has no upstream branch.\n"
80+
"To push the current branch and set the remote as upstream, use\n"
81+
"\n"
82+
" git push --set-upstream %s %s\n",
83+
branch->name,
84+
remote->name,
7585
branch->name);
7686
if (branch->merge_nr != 1)
7787
die("The current branch %s has multiple upstream branches, "
@@ -80,7 +90,7 @@ static void setup_push_upstream(void)
8090
add_refspec(refspec.buf);
8191
}
8292

83-
static void setup_default_push_refspecs(void)
93+
static void setup_default_push_refspecs(struct remote *remote)
8494
{
8595
switch (push_default) {
8696
default:
@@ -89,7 +99,7 @@ static void setup_default_push_refspecs(void)
8999
break;
90100

91101
case PUSH_DEFAULT_UPSTREAM:
92-
setup_push_upstream();
102+
setup_push_upstream(remote);
93103
break;
94104

95105
case PUSH_DEFAULT_CURRENT:
@@ -175,7 +185,7 @@ static int do_push(const char *repo, int flags)
175185
refspec = remote->push_refspec;
176186
refspec_nr = remote->push_refspec_nr;
177187
} else if (!(flags & TRANSPORT_PUSH_MIRROR))
178-
setup_default_push_refspecs();
188+
setup_default_push_refspecs(remote);
179189
}
180190
errs = 0;
181191
if (remote->pushurl_nr) {

0 commit comments

Comments
 (0)