Skip to content

Commit 02ac983

Browse files
rctaygitster
authored andcommitted
builtin/checkout: learn -B
Internally, --track and --orphan still use the 'safe' -b, not -B. Signed-off-by: Tay Ray Chuan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4c68875 commit 02ac983

File tree

3 files changed

+88
-7
lines changed

3 files changed

+88
-7
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

builtin/checkout.c

Lines changed: 24 additions & 5 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) {
@@ -659,6 +664,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
659664
OPT__QUIET(&opts.quiet),
660665
OPT_STRING('b', NULL, &opts.new_branch, "branch",
661666
"create and checkout a new branch"),
667+
OPT_STRING('B', NULL, &opts.new_branch_force, "branch",
668+
"create/reset and checkout a branch"),
662669
OPT_BOOLEAN('l', NULL, &opts.new_branch_log, "log for new branch"),
663670
OPT_SET_INT('t', "track", &opts.track, "track",
664671
BRANCH_TRACK_EXPLICIT),
@@ -689,6 +696,14 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
689696
argc = parse_options(argc, argv, prefix, options, checkout_usage,
690697
PARSE_OPT_KEEP_DASHDASH);
691698

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+
692707
if (patch_mode && (opts.track > 0 || opts.new_branch
693708
|| opts.new_branch_log || opts.merge || opts.force))
694709
die ("--patch is incompatible with all other options");
@@ -710,7 +725,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
710725

711726
if (opts.new_orphan_branch) {
712727
if (opts.new_branch)
713-
die("--orphan and -b are mutually exclusive");
728+
die("--orphan and -b|-B are mutually exclusive");
714729
if (opts.track > 0)
715730
die("--orphan cannot be used with -t");
716731
opts.new_branch = opts.new_orphan_branch;
@@ -859,8 +874,12 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
859874
if (strbuf_check_branch_ref(&buf, opts.new_branch))
860875
die("git checkout: we do not like '%s' as a branch name.",
861876
opts.new_branch);
862-
if (!get_sha1(buf.buf, rev))
863-
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+
}
864883
strbuf_release(&buf);
865884
}
866885

t/t2018-checkout-branch.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,49 @@ 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 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 with an explicit ref resets branch to that ref' '
128+
git checkout branch1 &&
129+
130+
do_checkout branch2 $HEAD1 -B
131+
'
132+
133+
test_expect_success 'checkout -B to an existing branch with unmergeable changes fails' '
134+
git checkout branch1 &&
135+
136+
setup_dirty_unmergeable &&
137+
test_must_fail do_checkout branch2 $HEAD1 -B &&
138+
test_dirty_unmergeable
139+
'
140+
141+
test_expect_success 'checkout -f -B to an existing branch with unmergeable changes discards changes' '
142+
# still dirty and on branch1
143+
do_checkout branch2 $HEAD1 "-f -B" &&
144+
test_must_fail test_dirty_unmergeable
145+
'
146+
147+
test_expect_success 'checkout -B to an existing branch preserves mergeable changes' '
148+
git checkout branch1 &&
149+
150+
setup_dirty_mergeable &&
151+
do_checkout branch2 $HEAD1 -B &&
152+
test_dirty_mergeable
153+
'
154+
155+
test_expect_success 'checkout -f -B to an existing branch with mergeable changes discards changes' '
156+
# clean up from previous test
157+
git reset --hard &&
158+
159+
git checkout branch1 &&
160+
161+
setup_dirty_mergeable &&
162+
do_checkout branch2 $HEAD1 "-f -B" &&
163+
test_must_fail test_dirty_mergeable
164+
'
165+
121166
test_done

0 commit comments

Comments
 (0)