Skip to content

Commit 07cd900

Browse files
committed
Merge branch 'tc/checkout-B'
* tc/checkout-B: builtin/checkout: handle -B from detached HEAD correctly builtin/checkout: learn -B builtin/checkout: reword hint for -b add tests for checkout -b
2 parents bb0a484 + cc70148 commit 07cd900

File tree

4 files changed

+218
-9
lines changed

4 files changed

+218
-9
lines changed

Documentation/git-checkout.txt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ SYNOPSIS
99
--------
1010
[verse]
1111
'git checkout' [-q] [-f] [-m] [<branch>]
12-
'git checkout' [-q] [-f] [-m] [[-b|--orphan] <new_branch>] [<start_point>]
12+
'git checkout' [-q] [-f] [-m] [[-b|-B|--orphan] <new_branch>] [<start_point>]
1313
'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>...
1414
'git checkout' --patch [<tree-ish>] [--] [<paths>...]
1515

@@ -21,7 +21,7 @@ also update `HEAD` to set the specified branch as the current
2121
branch.
2222

2323
'git checkout' [<branch>]::
24-
'git checkout' -b <new branch> [<start point>]::
24+
'git checkout' -b|-B <new_branch> [<start point>]::
2525

2626
This form switches branches by updating the index, working
2727
tree, and HEAD to reflect the specified branch.
@@ -31,6 +31,17 @@ were called and then checked out; in this case you can
3131
use the `--track` or `--no-track` options, which will be passed to
3232
'git branch'. As a convenience, `--track` without `-b` implies branch
3333
creation; see the description of `--track` below.
34+
+
35+
If `-B` is given, <new_branch> is created if it doesn't exist; otherwise, it
36+
is reset. This is the transactional equivalent of
37+
+
38+
------------
39+
$ git branch -f <branch> [<start point>]
40+
$ git checkout <branch>
41+
------------
42+
+
43+
that is to say, the branch is not reset/created unless "git checkout" is
44+
successful.
3445

3546
'git checkout' [--patch] [<tree-ish>] [--] <pathspec>...::
3647

@@ -75,6 +86,12 @@ entries; instead, unmerged entries are ignored.
7586
Create a new branch named <new_branch> and start it at
7687
<start_point>; see linkgit:git-branch[1] for details.
7788

89+
-B::
90+
Creates the branch <new_branch> and start it at <start_point>;
91+
if it already exists, then reset it to <start_point>. This is
92+
equivalent to running "git branch" with "-f"; see
93+
linkgit:git-branch[1] for details.
94+
7895
-t::
7996
--track::
8097
When creating a new branch, set up "upstream" configuration. See

branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ void create_branch(const char *head,
159159
dont_change_ref = 1;
160160
else if (!force)
161161
die("A branch named '%s' already exists.", name);
162-
else if (!is_bare_repository() && !strcmp(head, name))
162+
else if (!is_bare_repository() && head && !strcmp(head, name))
163163
die("Cannot force update the current branch.");
164164
forcing = 1;
165165
}

builtin/checkout.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ struct checkout_opts {
3232
int writeout_stage;
3333
int writeout_error;
3434

35+
/* not set by parse_options */
36+
int branch_exists;
37+
3538
const char *new_branch;
39+
const char *new_branch_force;
3640
const char *new_orphan_branch;
3741
int new_branch_log;
3842
enum branch_track track;
@@ -511,7 +515,8 @@ static void update_refs_for_switch(struct checkout_opts *opts,
511515
}
512516
}
513517
else
514-
create_branch(old->name, opts->new_branch, new->name, 0,
518+
create_branch(old->name, opts->new_branch, new->name,
519+
opts->new_branch_force ? 1 : 0,
515520
opts->new_branch_log, opts->track);
516521
new->name = opts->new_branch;
517522
setup_branch_path(new);
@@ -531,7 +536,7 @@ static void update_refs_for_switch(struct checkout_opts *opts,
531536
new->name);
532537
else
533538
fprintf(stderr, "Switched to%s branch '%s'\n",
534-
opts->new_branch ? " a new" : "",
539+
opts->branch_exists ? " and reset" : " a new",
535540
new->name);
536541
}
537542
if (old->path && old->name) {
@@ -657,7 +662,10 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
657662
int dwim_new_local_branch = 1;
658663
struct option options[] = {
659664
OPT__QUIET(&opts.quiet),
660-
OPT_STRING('b', NULL, &opts.new_branch, "new branch", "branch"),
665+
OPT_STRING('b', NULL, &opts.new_branch, "branch",
666+
"create and checkout a new branch"),
667+
OPT_STRING('B', NULL, &opts.new_branch_force, "branch",
668+
"create/reset and checkout a branch"),
661669
OPT_BOOLEAN('l', NULL, &opts.new_branch_log, "log for new branch"),
662670
OPT_SET_INT('t', "track", &opts.track, "track",
663671
BRANCH_TRACK_EXPLICIT),
@@ -688,6 +696,14 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
688696
argc = parse_options(argc, argv, prefix, options, checkout_usage,
689697
PARSE_OPT_KEEP_DASHDASH);
690698

699+
/* we can assume from now on new_branch = !new_branch_force */
700+
if (opts.new_branch && opts.new_branch_force)
701+
die("-B cannot be used with -b");
702+
703+
/* copy -B over to -b, so that we can just check the latter */
704+
if (opts.new_branch_force)
705+
opts.new_branch = opts.new_branch_force;
706+
691707
if (patch_mode && (opts.track > 0 || opts.new_branch
692708
|| opts.new_branch_log || opts.merge || opts.force))
693709
die ("--patch is incompatible with all other options");
@@ -709,7 +725,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
709725

710726
if (opts.new_orphan_branch) {
711727
if (opts.new_branch)
712-
die("--orphan and -b are mutually exclusive");
728+
die("--orphan and -b|-B are mutually exclusive");
713729
if (opts.track > 0)
714730
die("--orphan cannot be used with -t");
715731
opts.new_branch = opts.new_orphan_branch;
@@ -858,8 +874,12 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
858874
if (strbuf_check_branch_ref(&buf, opts.new_branch))
859875
die("git checkout: we do not like '%s' as a branch name.",
860876
opts.new_branch);
861-
if (!get_sha1(buf.buf, rev))
862-
die("git checkout: branch %s already exists", opts.new_branch);
877+
if (!get_sha1(buf.buf, rev)) {
878+
opts.branch_exists = 1;
879+
if (!opts.new_branch_force)
880+
die("git checkout: branch %s already exists",
881+
opts.new_branch);
882+
}
863883
strbuf_release(&buf);
864884
}
865885

t/t2018-checkout-branch.sh

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
#!/bin/sh
2+
3+
test_description='checkout '
4+
5+
. ./test-lib.sh
6+
7+
# Arguments: <branch> <sha> [<checkout options>]
8+
#
9+
# Runs "git checkout" to switch to <branch>, testing that
10+
#
11+
# 1) we are on the specified branch, <branch>;
12+
# 2) HEAD is <sha>; if <sha> is not specified, the old HEAD is used.
13+
#
14+
# If <checkout options> is not specified, "git checkout" is run with -b.
15+
do_checkout() {
16+
exp_branch=$1 &&
17+
exp_ref="refs/heads/$exp_branch" &&
18+
19+
# if <sha> is not specified, use HEAD.
20+
exp_sha=${2:-$(git rev-parse --verify HEAD)} &&
21+
22+
# default options for git checkout: -b
23+
if [ -z "$3" ]; then
24+
opts="-b"
25+
else
26+
opts="$3"
27+
fi
28+
29+
git checkout $opts $exp_branch $exp_sha &&
30+
31+
test $exp_ref = $(git rev-parse --symbolic-full-name HEAD) &&
32+
test $exp_sha = $(git rev-parse --verify HEAD)
33+
}
34+
35+
test_dirty_unmergeable() {
36+
! git diff --exit-code >/dev/null
37+
}
38+
39+
setup_dirty_unmergeable() {
40+
echo >>file1 change2
41+
}
42+
43+
test_dirty_mergeable() {
44+
! git diff --cached --exit-code >/dev/null
45+
}
46+
47+
setup_dirty_mergeable() {
48+
echo >file2 file2 &&
49+
git add file2
50+
}
51+
52+
test_expect_success 'setup' '
53+
test_commit initial file1 &&
54+
HEAD1=$(git rev-parse --verify HEAD) &&
55+
56+
test_commit change1 file1 &&
57+
HEAD2=$(git rev-parse --verify HEAD) &&
58+
59+
git branch -m branch1
60+
'
61+
62+
test_expect_success 'checkout -b to a new branch, set to HEAD' '
63+
do_checkout branch2
64+
'
65+
66+
test_expect_success 'checkout -b to a new branch, set to an explicit ref' '
67+
git checkout branch1 &&
68+
git branch -D branch2 &&
69+
70+
do_checkout branch2 $HEAD1
71+
'
72+
73+
test_expect_success 'checkout -b to a new branch with unmergeable changes fails' '
74+
git checkout branch1 &&
75+
76+
# clean up from previous test
77+
git branch -D branch2 &&
78+
79+
setup_dirty_unmergeable &&
80+
test_must_fail do_checkout branch2 $HEAD1 &&
81+
test_dirty_unmergeable
82+
'
83+
84+
test_expect_success 'checkout -f -b to a new branch with unmergeable changes discards changes' '
85+
# still dirty and on branch1
86+
do_checkout branch2 $HEAD1 "-f -b" &&
87+
test_must_fail test_dirty_unmergeable
88+
'
89+
90+
test_expect_success 'checkout -b to a new branch preserves mergeable changes' '
91+
git checkout branch1 &&
92+
93+
# clean up from previous test
94+
git branch -D branch2 &&
95+
96+
setup_dirty_mergeable &&
97+
do_checkout branch2 $HEAD1 &&
98+
test_dirty_mergeable
99+
'
100+
101+
test_expect_success 'checkout -f -b to a new branch with mergeable changes discards changes' '
102+
# clean up from previous test
103+
git reset --hard &&
104+
105+
git checkout branch1 &&
106+
107+
# clean up from previous test
108+
git branch -D branch2 &&
109+
110+
setup_dirty_mergeable &&
111+
do_checkout branch2 $HEAD1 "-f -b" &&
112+
test_must_fail test_dirty_mergeable
113+
'
114+
115+
test_expect_success 'checkout -b to an existing branch fails' '
116+
git reset --hard HEAD &&
117+
118+
test_must_fail do_checkout branch2 $HEAD2
119+
'
120+
121+
test_expect_success 'checkout -B to an existing branch resets branch to HEAD' '
122+
git checkout branch1 &&
123+
124+
do_checkout branch2 "" -B
125+
'
126+
127+
test_expect_success 'checkout -B to an existing branch from detached HEAD resets branch to HEAD' '
128+
git checkout $(git rev-parse --verify HEAD) &&
129+
130+
do_checkout branch2 "" -B
131+
'
132+
133+
test_expect_success 'checkout -B to an existing branch with an explicit ref resets branch to that ref' '
134+
git checkout branch1 &&
135+
136+
do_checkout branch2 $HEAD1 -B
137+
'
138+
139+
test_expect_success 'checkout -B to an existing branch with unmergeable changes fails' '
140+
git checkout branch1 &&
141+
142+
setup_dirty_unmergeable &&
143+
test_must_fail do_checkout branch2 $HEAD1 -B &&
144+
test_dirty_unmergeable
145+
'
146+
147+
test_expect_success 'checkout -f -B to an existing branch with unmergeable changes discards changes' '
148+
# still dirty and on branch1
149+
do_checkout branch2 $HEAD1 "-f -B" &&
150+
test_must_fail test_dirty_unmergeable
151+
'
152+
153+
test_expect_success 'checkout -B to an existing branch preserves mergeable changes' '
154+
git checkout branch1 &&
155+
156+
setup_dirty_mergeable &&
157+
do_checkout branch2 $HEAD1 -B &&
158+
test_dirty_mergeable
159+
'
160+
161+
test_expect_success 'checkout -f -B to an existing branch with mergeable changes discards changes' '
162+
# clean up from previous test
163+
git reset --hard &&
164+
165+
git checkout branch1 &&
166+
167+
setup_dirty_mergeable &&
168+
do_checkout branch2 $HEAD1 "-f -B" &&
169+
test_must_fail test_dirty_mergeable
170+
'
171+
172+
test_done

0 commit comments

Comments
 (0)