Skip to content

Commit 587a9ee

Browse files
ConradIrwingitster
authored andcommitted
Show interpreted branch name in error messages
Change the error message when doing: "git branch @{-1}", "git checkout -b @{-1}", or "git branch -m foo @{-1}" * was: A branch named '@{-1}' already exists. * now: A branch named 'bar' already exists. Signed-off-by: Conrad Irwin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 55c4a67 commit 587a9ee

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ int validate_new_branchname(const char *name, struct strbuf *ref, int force)
146146
if (!ref_exists(ref->buf))
147147
return 0;
148148
else if (!force)
149-
die("A branch named '%s' already exists.", name);
149+
die("A branch named '%s' already exists.", ref->buf + strlen("refs/heads/"));
150150

151151
head = resolve_ref("HEAD", sha1, 0, NULL);
152152
if (!is_bare_repository() && head && !strcmp(head, ref->buf))

t/t2018-checkout-branch.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ test_expect_success 'checkout -b to an existing branch fails' '
118118
test_must_fail do_checkout branch2 $HEAD2
119119
'
120120

121+
test_expect_success 'checkout -b to @{-1} fails with the right branch name' '
122+
git reset --hard HEAD &&
123+
git checkout branch1 &&
124+
git checkout branch2 &&
125+
echo >expect "fatal: A branch named '\''branch1'\'' already exists." &&
126+
test_must_fail git checkout -b @{-1} 2>actual &&
127+
test_cmp expect actual
128+
'
129+
121130
test_expect_success 'checkout -B to an existing branch resets branch to HEAD' '
122131
git checkout branch1 &&
123132

0 commit comments

Comments
 (0)