@@ -32,7 +32,11 @@ struct checkout_opts {
32
32
int writeout_stage ;
33
33
int writeout_error ;
34
34
35
+ /* not set by parse_options */
36
+ int branch_exists ;
37
+
35
38
const char * new_branch ;
39
+ const char * new_branch_force ;
36
40
const char * new_orphan_branch ;
37
41
int new_branch_log ;
38
42
enum branch_track track ;
@@ -511,7 +515,8 @@ static void update_refs_for_switch(struct checkout_opts *opts,
511
515
}
512
516
}
513
517
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 ,
515
520
opts -> new_branch_log , opts -> track );
516
521
new -> name = opts -> new_branch ;
517
522
setup_branch_path (new );
@@ -531,7 +536,7 @@ static void update_refs_for_switch(struct checkout_opts *opts,
531
536
new -> name );
532
537
else
533
538
fprintf (stderr , "Switched to%s branch '%s'\n" ,
534
- opts -> new_branch ? " a new " : "" ,
539
+ opts -> branch_exists ? " and reset " : " a new " ,
535
540
new -> name );
536
541
}
537
542
if (old -> path && old -> name ) {
@@ -659,6 +664,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
659
664
OPT__QUIET (& opts .quiet ),
660
665
OPT_STRING ('b' , NULL , & opts .new_branch , "branch" ,
661
666
"create and checkout a new branch" ),
667
+ OPT_STRING ('B' , NULL , & opts .new_branch_force , "branch" ,
668
+ "create/reset and checkout a branch" ),
662
669
OPT_BOOLEAN ('l' , NULL , & opts .new_branch_log , "log for new branch" ),
663
670
OPT_SET_INT ('t' , "track" , & opts .track , "track" ,
664
671
BRANCH_TRACK_EXPLICIT ),
@@ -689,6 +696,14 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
689
696
argc = parse_options (argc , argv , prefix , options , checkout_usage ,
690
697
PARSE_OPT_KEEP_DASHDASH );
691
698
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
+
692
707
if (patch_mode && (opts .track > 0 || opts .new_branch
693
708
|| opts .new_branch_log || opts .merge || opts .force ))
694
709
die ("--patch is incompatible with all other options" );
@@ -710,7 +725,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
710
725
711
726
if (opts .new_orphan_branch ) {
712
727
if (opts .new_branch )
713
- die ("--orphan and -b are mutually exclusive" );
728
+ die ("--orphan and -b|-B are mutually exclusive" );
714
729
if (opts .track > 0 )
715
730
die ("--orphan cannot be used with -t" );
716
731
opts .new_branch = opts .new_orphan_branch ;
@@ -859,8 +874,12 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
859
874
if (strbuf_check_branch_ref (& buf , opts .new_branch ))
860
875
die ("git checkout: we do not like '%s' as a branch name." ,
861
876
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
+ }
864
883
strbuf_release (& buf );
865
884
}
866
885
0 commit comments