@@ -714,7 +714,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
714
714
int delete = 0 , rename = 0 , force_create = 0 , list = 0 ;
715
715
int verbose = 0 , abbrev = -1 , detached = 0 ;
716
716
int reflog = 0 , edit_description = 0 ;
717
- int quiet = 0 ;
717
+ int quiet = 0 , unset_upstream = 0 ;
718
+ const char * new_upstream = NULL ;
718
719
enum branch_track track ;
719
720
int kinds = REF_LOCAL_BRANCH ;
720
721
struct commit_list * with_commit = NULL ;
@@ -728,6 +729,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
728
729
BRANCH_TRACK_EXPLICIT ),
729
730
OPT_SET_INT ( 0 , "set-upstream" , & track , N_ ("change upstream info" ),
730
731
BRANCH_TRACK_OVERRIDE ),
732
+ OPT_STRING ('u' , "set-upstream-to" , & new_upstream , "upstream" , "change the upstream info" ),
733
+ OPT_BOOLEAN (0 , "unset-upstream" , & unset_upstream , "Unset the upstream info" ),
731
734
OPT__COLOR (& branch_use_color , N_ ("use colored output" )),
732
735
OPT_SET_INT ('r' , "remotes" , & kinds , N_ ("act on remote-tracking branches" ),
733
736
REF_REMOTE_BRANCH ),
@@ -796,10 +799,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
796
799
argc = parse_options (argc , argv , prefix , options , builtin_branch_usage ,
797
800
0 );
798
801
799
- if (!delete && !rename && !edit_description && argc == 0 )
802
+ if (!delete && !rename && !edit_description && ! new_upstream && ! unset_upstream && argc == 0 )
800
803
list = 1 ;
801
804
802
- if (!!delete + !!rename + !!force_create + !!list > 1 )
805
+ if (!!delete + !!rename + !!force_create + !!list + !! new_upstream + !! unset_upstream > 1 )
803
806
usage_with_options (builtin_branch_usage , options );
804
807
805
808
if (abbrev == -1 )
@@ -854,11 +857,62 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
854
857
rename_branch (argv [0 ], argv [1 ], rename > 1 );
855
858
else
856
859
usage_with_options (builtin_branch_usage , options );
860
+ } else if (new_upstream ) {
861
+ struct branch * branch = branch_get (argv [0 ]);
862
+
863
+ if (!ref_exists (branch -> refname ))
864
+ die (_ ("branch '%s' does not exist" ), branch -> name );
865
+
866
+ /*
867
+ * create_branch takes care of setting up the tracking
868
+ * info and making sure new_upstream is correct
869
+ */
870
+ create_branch (head , branch -> name , new_upstream , 0 , 0 , 0 , quiet , BRANCH_TRACK_OVERRIDE );
871
+ } else if (unset_upstream ) {
872
+ struct branch * branch = branch_get (argv [0 ]);
873
+ struct strbuf buf = STRBUF_INIT ;
874
+
875
+ if (!branch_has_merge_config (branch )) {
876
+ die (_ ("Branch '%s' has no upstream information" ), branch -> name );
877
+ }
878
+
879
+ strbuf_addf (& buf , "branch.%s.remote" , branch -> name );
880
+ git_config_set_multivar (buf .buf , NULL , NULL , 1 );
881
+ strbuf_reset (& buf );
882
+ strbuf_addf (& buf , "branch.%s.merge" , branch -> name );
883
+ git_config_set_multivar (buf .buf , NULL , NULL , 1 );
884
+ strbuf_release (& buf );
857
885
} else if (argc > 0 && argc <= 2 ) {
886
+ struct branch * branch = branch_get (argv [0 ]);
887
+ int branch_existed = 0 , remote_tracking = 0 ;
888
+ struct strbuf buf = STRBUF_INIT ;
889
+
858
890
if (kinds != REF_LOCAL_BRANCH )
859
891
die (_ ("-a and -r options to 'git branch' do not make sense with a branch name" ));
892
+
893
+ if (track == BRANCH_TRACK_OVERRIDE )
894
+ fprintf (stderr , _ ("The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to\n" ));
895
+
896
+ strbuf_addf (& buf , "refs/remotes/%s" , branch -> name );
897
+ remote_tracking = ref_exists (buf .buf );
898
+ strbuf_release (& buf );
899
+
900
+ branch_existed = ref_exists (branch -> refname );
860
901
create_branch (head , argv [0 ], (argc == 2 ) ? argv [1 ] : head ,
861
902
force_create , reflog , 0 , quiet , track );
903
+
904
+ /*
905
+ * We only show the instructions if the user gave us
906
+ * one branch which doesn't exist locally, but is the
907
+ * name of a remote-tracking branch.
908
+ */
909
+ if (argc == 1 && track == BRANCH_TRACK_OVERRIDE &&
910
+ !branch_existed && remote_tracking ) {
911
+ fprintf (stderr , _ ("\nIf you wanted to make '%s' track '%s', do this:\n\n" ), head , branch -> name );
912
+ fprintf (stderr , _ (" git branch -d %s\n" ), branch -> name );
913
+ fprintf (stderr , _ (" git branch --set-upstream-to %s\n" ), branch -> name );
914
+ }
915
+
862
916
} else
863
917
usage_with_options (builtin_branch_usage , options );
864
918
0 commit comments