git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/2 v2] push: better error messages when push.default = tracking

From
Matthieu Moy <matthieu.moy@imag.fr>
Date
Mar 1, 2011, 09:11 UTC
Message-ID
<1298970662-25828-1-git-send-email-Matthieu.Moy@imag.fr>
In-Reply-To
<1298927645-2716-1-git-send-email-Matthieu.Moy@imag.fr>

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 <Matthieu.Moy@imag.fr>
---
Compared to v1, the detached HEAD case joined this patch since it
became semantically linked (requires the "remote" extra parameter),
and doesn't mention the possibility to checkout another branch before
pushing it (cf. Junio).
 builtin/push.c |   21 +++++++++++++++------
 1 files changed, 15 insertions(+), 6 deletions(-)
diff --git a/builtin/push.c b/builtin/push.c
index 31da418..c949187 100644
--- a/builtin/push.c
+++ b/builtin/push.c
@@ -64,14 +64,23 @@ static void set_refspecs(const char **refs, int nr)
 	}
 }
 
-static void setup_push_upstream(void)
+static void setup_push_upstream(struct remote *remote)
 {
 	struct strbuf refspec = STRBUF_INIT;
 	struct branch *branch = branch_get(NULL);
 	if (!branch)
-		die("You are not currently on a branch.");
+		die("You are not currently on a branch (detached HEAD).\n"
+		    "To push a specific branch and set the remote as upstream, use\n"
+		    "\n"
+		    "    git push --set-upstream %s <branch-name>\n",
+		    remote->name);
 	if (!branch->merge_nr || !branch->merge)
-		die("The current branch %s has no upstream branch.",
+		die("The current branch %s has no upstream branch.\n"
+		    "To push the current branch and set the remote as upstream, use\n"
+		    "\n"
+		    "    git push --set-upstream %s %s\n",
+		    branch->name,
+		    remote->name,
 		    branch->name);
 	if (branch->merge_nr != 1)
 		die("The current branch %s has multiple upstream branches, "
@@ -80,7 +89,7 @@ static void setup_push_upstream(void)
 	add_refspec(refspec.buf);
 }
 
-static void setup_default_push_refspecs(void)
+static void setup_default_push_refspecs(struct remote *remote)
 {
 	switch (push_default) {
 	default:
@@ -89,7 +98,7 @@ static void setup_default_push_refspecs(void)
 		break;
 
 	case PUSH_DEFAULT_UPSTREAM:
-		setup_push_upstream();
+		setup_push_upstream(remote);
 		break;
 
 	case PUSH_DEFAULT_CURRENT:
@@ -175,7 +184,7 @@ static int do_push(const char *repo, int flags)
 			refspec = remote->push_refspec;
 			refspec_nr = remote->push_refspec_nr;
 		} else if (!(flags & TRANSPORT_PUSH_MIRROR))
-			setup_default_push_refspecs();
+			setup_default_push_refspecs(remote);
 	}
 	errs = 0;
 	if (remote->pushurl_nr) {
-- 
1.7.4.1.142.ge03e1
Previous: Junio C HamanoNext: Matthieu Moy
Message 7 of 12 in “push: better error messages”
  1. 0/2 push: better error messagesMatthieu Moy, Feb 28, 2011
  2. 1/2 push: better error message when push.default = trackingMatthieu Moy, Feb 28, 2011
  3. 2/2 push: better error messages for detached HEAD and "no destination"Matthieu Moy, Feb 28, 2011
  4. Jay SoffianFeb 28, 2011
  5. Matthieu MoyFeb 28, 2011
  6. Junio C HamanoFeb 28, 2011
  7. 1/2 push: better error messages when push.default = trackingMatthieu Moy, Mar 1, 2011
  8. 2/2 push: better error message when no remote configuredMatthieu Moy, Mar 1, 2011
  9. Junio C HamanoMar 1, 2011
  10. 1/2 push: better error messages when push.default = trackingMatthieu Moy, Mar 2, 2011
  11. 2/2 push: better error message when no remote configuredMatthieu Moy, Mar 2, 2011
  12. Junio C HamanoMar 3, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.