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

Re: git-branch -m interprets first argument differently when two are supplied

From
Johannes Sixt <j.sixt@viscovery.net>
Date
Feb 14, 2008, 07:37 UTC
Message-ID
<47B3EFC4.1020808@viscovery.net>
In-Reply-To
<47B3497A.9050703@nrlssc.navy.mil>
Brandon Casey schrieb:
Show 7 quoted lines
> The first non-option argument is interpreted differently depending on
> whether one argument or two arguments have been supplied.
> 
> 	git-branch -m [<oldbranch>] <newbranch>
> 
> Has anyone considered whether this is inconsistent with how other
> commands operate?

Funny, I fell into this trap just yesterday and accidentally renamed my master branch to something else. IMO git-branch -m should take two arguments. Full stop.

Something like this.
-- >8 --
From: Johannes Sixt <johannes.sixt@telecom.at>
Subject: [PATCH] git-branch: Deprecate -m with only one argument.

"git branch -m" can be invoked with only one argument, in which case it renames the current branch to the new name. This is inconsistent since the first argument is either a source or destination depending on whether a second argument follows. Furthermore, it is surprising that a rename command that is given only one argument succeeds at all.

Let's aim for removing this feature.
Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
---
 Documentation/git-branch.txt |    4 ++--
 builtin-branch.c             |    9 ++++++---
 2 files changed, 8 insertions(+), 5 deletions(-)
diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index 7e8874a..56bea06 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -12,7 +12,7 @@ SYNOPSIS
 	   [-v [--abbrev=<length> | --no-abbrev]]
 	   [--contains <commit>]
 'git-branch' [--track | --no-track] [-l] [-f] <branchname> [<start-point>]
-'git-branch' (-m | -M) [<oldbranch>] <newbranch>
+'git-branch' (-m | -M) <oldbranch> <newbranch>
 'git-branch' (-d | -D) [-r] <branchname>...

 DESCRIPTION
@@ -136,7 +136,7 @@ OPTIONS

 <newbranch>::
 	The new name for an existing branch. The same restrictions as for
-	<branchname> applies.
+	<branchname> apply.


 Examples
diff --git a/builtin-branch.c b/builtin-branch.c
index e414c88..14df594 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -17,7 +17,7 @@ static const char * const builtin_branch_usage[] = {
 	"git-branch [options] [-r | -a]",
 	"git-branch [options] [-l] [-f] <branchname> [<start-point>]",
 	"git-branch [options] [-r] (-d | -D) <branchname>",
-	"git-branch [options] (-m | -M) [<oldbranch>] <newbranch>",
+	"git-branch [options] (-m | -M) <oldbranch> <newbranch>",
 	NULL
 };

@@ -609,9 +609,12 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		return delete_branches(argc, argv, delete > 1, kinds);
 	else if (argc == 0)
 		print_ref_list(kinds, detached, verbose, abbrev, with_commit);
-	else if (rename && (argc == 1))
+	else if (rename && (argc == 1)) {
+		fprintf(stderr, "'git branch -m' with only one parameter is deprecated.\n"
+			"Will continue, assuming that you meant\n"
+			"    git branch -m %s %s\n", head, argv[0]);
 		rename_branch(head, argv[0], rename > 1);
-	else if (rename && (argc == 2))
+	} else if (rename && (argc == 2))
 		rename_branch(argv[0], argv[1], rename > 1);
 	else if (argc <= 2)
 		create_branch(argv[0], (argc == 2) ? argv[1] : head,
-- 
1.5.4.1.104.g84d88
Previous: Brandon CaseyNext: Brandon Casey
Message 2 of 6 in “git-branch -m interprets first argument differently when two are supplied”
  1. Brandon CaseyFeb 13, 2008
  2. Johannes SixtFeb 14, 2008
  3. Brandon CaseyFeb 14, 2008
  4. Johannes SchindelinFeb 14, 2008
  5. Brandon CaseyFeb 14, 2008
  6. Johannes SchindelinFeb 14, 2008

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.