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

[PATCH] switch: rename --create to --new

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Jul 9, 2021, 17:43 UTC
Message-ID
<20210709174310.94209-1-felipe.contreras@gmail.com>

-c is used by `git branch` to copy a branch, it would be nice to copy the same options for consistency.

Fortunately there's a pretty good alternative to create a new branch: --new.

-c and --create are still supported, but deprecated.
Suggested-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 Documentation/git-switch.txt |  8 ++++++--
 builtin/checkout.c           |  2 ++
 t/t2060-switch.sh            | 13 +++++++++++--
 3 files changed, 19 insertions(+), 4 deletions(-)
diff --git a/Documentation/git-switch.txt b/Documentation/git-switch.txt
index 5c438cd505..67f8add9a7 100644
--- a/Documentation/git-switch.txt
+++ b/Documentation/git-switch.txt
@@ -56,8 +56,8 @@ As a special case, you may use `A...B` as a shortcut for the merge
 base of `A` and `B` if there is exactly one merge base. You can leave
 out at most one of `A` and `B`, in which case it defaults to `HEAD`.
 
--c <new-branch>::
---create <new-branch>::
+-n <new-branch>::
+--new <new-branch>::
 	Create a new branch named `<new-branch>` starting at
 	`<start-point>` before switching to the branch. This is a
 	convenient shortcut for:
@@ -67,6 +67,10 @@ $ git branch <new-branch>
 $ git switch <new-branch>
 ------------
 
+-c <new-branch>::
+--create <new-branch>::
+Deprecated alias for --new.
+
 -C <new-branch>::
 --force-create <new-branch>::
 	Similar to `--create` except that if `<new-branch>` already
diff --git a/builtin/checkout.c b/builtin/checkout.c
index f4cd7747d3..43364b5b00 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1833,6 +1833,8 @@ int cmd_switch(int argc, const char **argv, const char *prefix)
 	struct checkout_opts opts;
 	struct option *options = NULL;
 	struct option switch_options[] = {
+		OPT_STRING('n', "new", &opts.new_branch, N_("branch"),
+			   N_("create and switch to a new branch")),
 		OPT_STRING('c', "create", &opts.new_branch, N_("branch"),
 			   N_("create and switch to a new branch")),
 		OPT_STRING('C', "force-create", &opts.new_branch_force, N_("branch"),
diff --git a/t/t2060-switch.sh b/t/t2060-switch.sh
index 9bc6a3aa5c..e6023c70f7 100755
--- a/t/t2060-switch.sh
+++ b/t/t2060-switch.sh
@@ -41,17 +41,26 @@ test_expect_success 'switch and detach current branch' '
 
 test_expect_success 'switch and create branch' '
 	test_when_finished git switch main &&
-	git switch -c temp main^ &&
+	git switch -n temp main^ &&
 	test_cmp_rev main^ refs/heads/temp &&
 	echo refs/heads/temp >expected-branch &&
 	git symbolic-ref HEAD >actual-branch &&
 	test_cmp expected-branch actual-branch
 '
 
+test_expect_success 'switch and create branch (deprecated)' '
+	test_when_finished git switch main &&
+	git switch -c temp-d main^ &&
+	test_cmp_rev main^ refs/heads/temp-d &&
+	echo refs/heads/temp-d >expected-branch &&
+	git symbolic-ref HEAD >actual-branch &&
+	test_cmp expected-branch actual-branch
+'
+
 test_expect_success 'force create branch from HEAD' '
 	test_when_finished git switch main &&
 	git switch --detach main &&
-	test_must_fail git switch -c temp &&
+	test_must_fail git switch -n temp &&
 	git switch -C temp &&
 	test_cmp_rev main refs/heads/temp &&
 	echo refs/heads/temp >expected-branch &&
-- 
2.32.0.36.g70aac2b1aa
Message 1 of 1 in “switch: rename --create to --new”
  1. switch: rename --create to --newFelipe Contreras, Jul 9, 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.