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

[PATCH] branch: delete now accepts '-' as branch name

From
EGErlend Egeberg Aasland via GitGitGadget <gitgitgadget@gmail.com>
Date
Feb 16, 2022, 14:08 UTC
Message-ID
<pull.1217.git.git.1645020495014.gitgitgadget@gmail.com>
From: "Erlend E. Aasland" <erlend.aasland@innova.no>
This makes it easy to get rid of short-lived branches:

$ git switch -c experiment $ git switch - $ git branch -D -

$ gh pr checkout nnn $ make && make test $ git switch - $ git branch -D -

Signed-off-by: Erlend E. Aasland <erlend.aasland@innova.no>
---
    [RFC] branch: delete now accepts '-' as branch name
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1217%2Ferlend-aasland%2Fbranch-name-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1217/erlend-aasland/branch-name-v1
Pull-Request: https://github.com/git/git/pull/1217
 builtin/branch.c  | 6 +++++-
 t/t3200-branch.sh | 8 ++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
diff --git a/builtin/branch.c b/builtin/branch.c
index 4ce2a247542..f6c876c09d2 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -236,7 +236,11 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 		char *target = NULL;
 		int flags = 0;
 
-		strbuf_branchname(&bname, argv[i], allowed_interpret);
+		if (!strcmp(argv[i], "-")) {
+			strbuf_branchname(&bname, "@{-1}", allowed_interpret);
+		} else {
+			strbuf_branchname(&bname, argv[i], allowed_interpret);
+		}
 		free(name);
 		name = mkpathdup(fmt, bname.buf);
 
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index 1bc3795847d..6afb7150534 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -71,6 +71,14 @@ test_expect_success 'git branch -d d/e/f should delete a branch and a log' '
 	test_must_fail git reflog exists refs/heads/d/e/f
 '
 
+test_expect_success 'git branch -D - should delete branch @{-1}' '
+	git switch -c j &&
+	git switch - &&
+	git branch -D - &&
+	test_path_is_missing .git/refs/heads/j &&
+	test_must_fail git reflog exists refs/heads/j
+'
+
 test_expect_success 'git branch j/k should work after branch j has been deleted' '
 	git branch j &&
 	git branch -d j &&

base-commit: b80121027d1247a0754b3cc46897fee75c050b44
-- 
gitgitgadget
Next: Junio C Hamano
Message 1 of 11 in “branch: delete now accepts '-' as branch name”
  1. branch: delete now accepts '-' as branch nameErlend Egeberg Aasland via GitGitGadget, Feb 16, 2022
  2. Junio C HamanoFeb 16, 2022
  3. Eric SunshineFeb 16, 2022
  4. Junio C HamanoFeb 16, 2022
  5. Erlend AaslandFeb 16, 2022
  6. Eric SunshineFeb 17, 2022
  7. Junio C HamanoFeb 17, 2022
  8. Ævar Arnfjörð BjarmasonFeb 21, 2022
  9. Junio C HamanoFeb 21, 2022
  10. Ævar Arnfjörð BjarmasonFeb 21, 2022
  11. Erlend AaslandFeb 22, 2022

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.