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

[PATCH v2 2/2] push: make default consistent

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Jul 19, 2021, 03:47 UTC
Message-ID
<20210719034758.1288766-3-felipe.contreras@gmail.com>
In-Reply-To
<20210719034758.1288766-1-felipe.contreras@gmail.com>

The default action "simple" doesn't make sense. It's supposed to be the same as "current", except with an extra safety in case the name of the upstream branch doesn't match the name of the current branch (and we are pushing to the same remote). But if there's no upstream configured there's no need for any check.

Instead of behaving like "current" if the current branch doesn't have an upstream configured, `git push` fails like "upstream", so it's a Frankensteinian action.

If the upstream branch isn't configured, "simple" should not abort, just like "current".

Reported-by: Mathias Kunter <mathiaskunter@gmail.com>
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 Documentation/config/push.txt |  5 +++--
 Documentation/git-push.txt    |  6 +++---
 builtin/push.c                | 12 +++---------
 t/t5528-push-default.sh       |  2 +-
 4 files changed, 10 insertions(+), 15 deletions(-)
diff --git a/Documentation/config/push.txt b/Documentation/config/push.txt
index 632033638c..d267d05e7a 100644
--- a/Documentation/config/push.txt
+++ b/Documentation/config/push.txt
@@ -27,8 +27,9 @@ push.default::
 * `simple` - pushes the current branch with the same name on the remote.
 +
 If you are working on a centralized workflow (pushing to the same repository you
-pull from, which is typically `origin`), then you need to configure an upstream
-branch with the same name.
+pull from, which is typically `origin`), and you have configured an upstream
+branch, then the name must be the same as the current branch, otherwise this
+action will fail as a precaution.
 +
 This mode is the default since Git 2.0, and is the safest option suited for
 beginners.
diff --git a/Documentation/git-push.txt b/Documentation/git-push.txt
index a953c7c387..58352bbf88 100644
--- a/Documentation/git-push.txt
+++ b/Documentation/git-push.txt
@@ -39,9 +39,9 @@ what to push (See linkgit:git-config[1] for the meaning of `push.default`).
 
 When neither the command-line nor the configuration specify what to
 push, the default behavior is used, which corresponds to the `simple`
-value for `push.default`: the current branch is pushed to the
-corresponding upstream branch, but as a safety measure, the push is
-aborted if the upstream branch does not have the same name as the
+value for `push.default`: the current branch is pushed to a remote
+branch with the same name, but as a safety measure the push is aborted
+if there's a configured upstream branch with a different name than the
 local one.
 
 
diff --git a/builtin/push.c b/builtin/push.c
index 3ee7d6ada1..64134531de 100644
--- a/builtin/push.c
+++ b/builtin/push.c
@@ -229,16 +229,10 @@ static void setup_default_push_refspecs(struct remote *remote)
 		if (!same_remote)
 			break;
 		dst = get_upstream_ref(branch);
-		if (!dst)
-			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 (strcmp(branch->refname, dst))
+		if (dst && strcmp(branch->refname, dst))
 			die_push_simple(branch, remote);
+		if (!dst)
+			dst = branch->refname;
 		break;
 
 	case PUSH_DEFAULT_UPSTREAM:
diff --git a/t/t5528-push-default.sh b/t/t5528-push-default.sh
index f280e00eb7..ba38256ef4 100755
--- a/t/t5528-push-default.sh
+++ b/t/t5528-push-default.sh
@@ -126,7 +126,7 @@ test_expect_success 'push from/to new branch with current creates remote branch'
 test_expect_success 'push to existing branch, with no upstream configured' '
 	test_config branch.main.remote repo1 &&
 	git checkout main &&
-	test_push_failure simple &&
+	test_push_success simple main &&
 	test_push_failure upstream
 '
 
-- 
2.32.0.40.gb9b36f9b52
Previous: Felipe Contreras
Message 4 of 4 in “push: make default consistent”
  1. push: make default consistentFelipe Contreras, Jul 14, 2021
  2. 0/2 push: fix default actionFelipe Contreras, Jul 19, 2021
  3. 1/2 push: reorganize get_upstream_refFelipe Contreras, Jul 19, 2021
  4. 2/2 push: make default consistentFelipe Contreras, Jul 19, 2021

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.