Skip to content

Commit bb2198f

Browse files
rscharfegitster
authored andcommitted
checkout: improve error messages for -b with extra argument
When we try to create a branch "foo" based on "origin/master" and give git commit -b an extra unsupported argument "bar", it confusingly reports: $ git checkout -b foo origin/master bar fatal: 'bar' is not a commit and a branch 'foo' cannot be created from it $ git checkout --track -b foo origin/master bar fatal: 'bar' is not a commit and a branch 'foo' cannot be created from it That's wrong, because it very well understands that "origin/master" is supposed to be the start point for the new branch and not "bar". Check if we got a commit and show more fitting messages in that case instead: $ git checkout -b foo origin/master bar fatal: Cannot update paths and switch to branch 'foo' at the same time. $ git checkout --track -b foo origin/master bar fatal: '--track' cannot be used with updating paths Original-patch-by: Jeff King <[email protected]> Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 16ab794 commit bb2198f

File tree

3 files changed

+3
-3
lines changed

3 files changed

+3
-3
lines changed

builtin/checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1689,7 +1689,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
16891689
* Try to give more helpful suggestion.
16901690
* new_branch && argc > 1 will be caught later.
16911691
*/
1692-
if (opts->new_branch && argc == 1)
1692+
if (opts->new_branch && argc == 1 && !new_branch_info.commit)
16931693
die(_("'%s' is not a commit and a branch '%s' cannot be created from it"),
16941694
argv[0], opts->new_branch);
16951695

t/t2018-checkout-branch.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ test_expect_success 'checkout -b rejects an invalid start point' '
265265
test_i18ngrep "is not a commit" err
266266
'
267267

268-
test_expect_failure 'checkout -b rejects an extra path argument' '
268+
test_expect_success 'checkout -b rejects an extra path argument' '
269269
test_must_fail git checkout -b branch5 branch1 file1 2>err &&
270270
test_i18ngrep "Cannot update paths and switch to branch" err
271271
'

t/t2027-checkout-track.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ test_expect_success 'checkout --track -b creates a new tracking branch' '
1616
test $(git config --get branch.branch1.merge) = refs/heads/master
1717
'
1818

19-
test_expect_failure 'checkout --track -b rejects an extra path argument' '
19+
test_expect_success 'checkout --track -b rejects an extra path argument' '
2020
test_must_fail git checkout --track -b branch2 master one.t 2>err &&
2121
test_i18ngrep "cannot be used with updating paths" err
2222
'

0 commit comments

Comments
 (0)