@@ -889,6 +889,17 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
889889 } else if (new_upstream ) {
890890 struct branch * branch = branch_get (argv [0 ]);
891891
892+ if (argc > 1 )
893+ die (_ ("too many branches to set new upstream" ));
894+
895+ if (!branch ) {
896+ if (!argc || !strcmp (argv [0 ], "HEAD" ))
897+ die (_ ("could not set upstream of HEAD to %s when "
898+ "it does not point to any branch." ),
899+ new_upstream );
900+ die (_ ("no such branch '%s'" ), argv [0 ]);
901+ }
902+
892903 if (!ref_exists (branch -> refname ))
893904 die (_ ("branch '%s' does not exist" ), branch -> name );
894905
@@ -901,6 +912,16 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
901912 struct branch * branch = branch_get (argv [0 ]);
902913 struct strbuf buf = STRBUF_INIT ;
903914
915+ if (argc > 1 )
916+ die (_ ("too many branches to unset upstream" ));
917+
918+ if (!branch ) {
919+ if (!argc || !strcmp (argv [0 ], "HEAD" ))
920+ die (_ ("could not unset upstream of HEAD when "
921+ "it does not point to any branch." ));
922+ die (_ ("no such branch '%s'" ), argv [0 ]);
923+ }
924+
904925 if (!branch_has_merge_config (branch )) {
905926 die (_ ("Branch '%s' has no upstream information" ), branch -> name );
906927 }
@@ -916,6 +937,12 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
916937 int branch_existed = 0 , remote_tracking = 0 ;
917938 struct strbuf buf = STRBUF_INIT ;
918939
940+ if (!strcmp (argv [0 ], "HEAD" ))
941+ die (_ ("it does not make sense to create 'HEAD' manually" ));
942+
943+ if (!branch )
944+ die (_ ("no such branch '%s'" ), argv [0 ]);
945+
919946 if (kinds != REF_LOCAL_BRANCH )
920947 die (_ ("-a and -r options to 'git branch' do not make sense with a branch name" ));
921948
0 commit comments