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

[PATCH] "git branch -M @{-1}"

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 27, 2009, 06:12 UTC
Message-ID
<7vprh4o1bt.fsf_-_@gitster.siamese.dyndns.org>
In-Reply-To
<7veixkpi4a.fsf@gitster.siamese.dyndns.org>

Earlier, we've taught "git branch -d <name>" that <name> is supposed to be a branch name and not a random extended SHA-1. "git branch -m <name> [<name>]" needs to be taught the same.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin-branch.c |   14 ++++++++++----
 1 files changed, 10 insertions(+), 4 deletions(-)
diff --git a/builtin-branch.c b/builtin-branch.c
index c34af27..3f97838 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -425,21 +425,27 @@ static void rename_branch(const char *oldname, const char *newname, int force)
 	if (!oldname)
 		die("cannot rename the current branch while not on any.");
 
-	strbuf_addf(&oldref, "refs/heads/%s", oldname);
+	if (interpret_nth_last_branch(oldname, &oldref) == strlen(oldname))
+		strbuf_splice(&oldref, 0, 0, "refs/heads/", 11);
+	else
+		strbuf_addf(&oldref, "refs/heads/%s", oldname);
 
 	if (check_ref_format(oldref.buf))
 		die("Invalid branch name: %s", oldref.buf);
 
-	strbuf_addf(&newref, "refs/heads/%s", newname);
+	if (interpret_nth_last_branch(newname, &newref) == strlen(newname))
+		strbuf_splice(&newref, 0, 0, "refs/heads/", 11);
+	else
+		strbuf_addf(&newref, "refs/heads/%s", newname);
 
 	if (check_ref_format(newref.buf))
 		die("Invalid branch name: %s", newref.buf);
 
 	if (resolve_ref(newref.buf, sha1, 1, NULL) && !force)
-		die("A branch named '%s' already exists.", newname);
+		die("A branch named '%s' already exists.", newref.buf + 11);
 
 	strbuf_addf(&logmsg, "Branch: renamed %s to %s",
-		 oldref.buf, newref.buf);
+		    oldref.buf, newref.buf);
 
 	if (rename_ref(oldref.buf, newref.buf, logmsg.buf))
 		die("Branch rename failed");
Previous: Junio C HamanoNext: Junio C Hamano
Message 20 of 21 in “teach the new "@{-1} syntax to "git branch"”
  1. teach the new "@{-1} syntax to "git branch"Junio C Hamano, Feb 14, 2009
  2. Jay SoffianFeb 14, 2009
  3. Junio C HamanoFeb 14, 2009
  4. 1/2 Teach the "@{-1} syntax to "git branch"Junio C Hamano, Feb 14, 2009
  5. 2/2 Teach @{-1} to git mergeJunio C Hamano, Feb 14, 2009
  6. Johannes SchindelinFeb 14, 2009
  7. Junio C HamanoFeb 15, 2009
  8. Junio C HamanoFeb 15, 2009
  9. Nanako ShiraishiFeb 15, 2009
  10. Junio C HamanoFeb 15, 2009
  11. Johannes SchindelinFeb 15, 2009
  12. Junio C HamanoFeb 16, 2009
  13. Johannes SchindelinFeb 16, 2009
  14. Jay SoffianFeb 16, 2009
  15. Junio C HamanoFeb 26, 2009
  16. Johannes SchindelinFeb 26, 2009
  17. Deskin MillerFeb 27, 2009
  18. Junio C HamanoFeb 27, 2009
  19. git-branch: a detached HEAD and branch called HEAD are differentJunio C Hamano, Feb 27, 2009
  20. "git branch -M @{-1}"Junio C Hamano, Feb 27, 2009
  21. Junio C HamanoFeb 27, 2009

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.