Skip to content

Commit 39bd6f7

Browse files
jrngitster
authored andcommitted
Allow checkout -B <current-branch> to update the current branch
When on master, "git checkout -B master <commit>" is a more natural way to say "git reset --keep <commit>", which was originally invented for the exact purpose of moving to the named commit while keeping the local changes around. Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3f59481 commit 39bd6f7

File tree

5 files changed

+23
-12
lines changed

5 files changed

+23
-12
lines changed

branch.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ int validate_new_branchname(const char *name, struct strbuf *ref,
160160

161161
void create_branch(const char *head,
162162
const char *name, const char *start_name,
163-
int force, int reflog, enum branch_track track)
163+
int force, int reflog, int clobber_head,
164+
enum branch_track track)
164165
{
165166
struct ref_lock *lock = NULL;
166167
struct commit *commit;
@@ -175,7 +176,8 @@ void create_branch(const char *head,
175176
explicit_tracking = 1;
176177

177178
if (validate_new_branchname(name, &ref, force,
178-
track == BRANCH_TRACK_OVERRIDE)) {
179+
track == BRANCH_TRACK_OVERRIDE ||
180+
clobber_head)) {
179181
if (!force)
180182
dont_change_ref = 1;
181183
else

branch.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
* branch for (if any).
1414
*/
1515
void create_branch(const char *head, const char *name, const char *start_name,
16-
int force, int reflog, enum branch_track track);
16+
int force, int reflog,
17+
int clobber_head, enum branch_track track);
1718

1819
/*
1920
* Validates that the requested branch may be created, returning the

builtin/branch.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
737737
if (kinds != REF_LOCAL_BRANCH)
738738
die(_("-a and -r options to 'git branch' do not make sense with a branch name"));
739739
create_branch(head, argv[0], (argc == 2) ? argv[1] : head,
740-
force_create, reflog, track);
740+
force_create, reflog, 0, track);
741741
} else
742742
usage_with_options(builtin_branch_usage, options);
743743

builtin/checkout.c

+11-4
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,9 @@ static void update_refs_for_switch(struct checkout_opts *opts,
540540
else
541541
create_branch(old->name, opts->new_branch, new->name,
542542
opts->new_branch_force ? 1 : 0,
543-
opts->new_branch_log, opts->track);
543+
opts->new_branch_log,
544+
opts->new_branch_force ? 1 : 0,
545+
opts->track);
544546
new->name = opts->new_branch;
545547
setup_branch_path(new);
546548
}
@@ -565,8 +567,12 @@ static void update_refs_for_switch(struct checkout_opts *opts,
565567
create_symref("HEAD", new->path, msg.buf);
566568
if (!opts->quiet) {
567569
if (old->path && !strcmp(new->path, old->path)) {
568-
fprintf(stderr, _("Already on '%s'\n"),
569-
new->name);
570+
if (opts->new_branch_force)
571+
fprintf(stderr, _("Reset branch '%s'\n"),
572+
new->name);
573+
else
574+
fprintf(stderr, _("Already on '%s'\n"),
575+
new->name);
570576
} else if (opts->new_branch) {
571577
if (opts->branch_exists)
572578
fprintf(stderr, _("Switched to and reset branch '%s'\n"), new->name);
@@ -1057,7 +1063,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
10571063
struct strbuf buf = STRBUF_INIT;
10581064

10591065
opts.branch_exists = validate_new_branchname(opts.new_branch, &buf,
1060-
!!opts.new_branch_force, 0);
1066+
!!opts.new_branch_force,
1067+
!!opts.new_branch_force);
10611068

10621069
strbuf_release(&buf);
10631070
}

t/t2018-checkout-branch.sh

+5-4
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,13 @@ test_expect_success 'checkout -b <describe>' '
189189
test_cmp expect actual
190190
'
191191

192-
test_expect_success 'checkout -B to the current branch fails before merging' '
192+
test_expect_success 'checkout -B to the current branch works' '
193193
git checkout branch1 &&
194+
git checkout -B branch1-scratch &&
195+
194196
setup_dirty_mergeable &&
195-
git commit -mfooble &&
196-
test_must_fail git checkout -B branch1 initial &&
197-
test_must_fail test_dirty_mergeable
197+
git checkout -B branch1-scratch initial &&
198+
test_dirty_mergeable
198199
'
199200

200201
test_done

0 commit comments

Comments
 (0)