@@ -570,6 +570,7 @@ static void rename_branch(const char *oldname, const char *newname, int force)
570570 struct strbuf oldref = STRBUF_INIT , newref = STRBUF_INIT , logmsg = STRBUF_INIT ;
571571 struct strbuf oldsection = STRBUF_INIT , newsection = STRBUF_INIT ;
572572 int recovery = 0 ;
573+ int clobber_head_ok ;
573574
574575 if (!oldname )
575576 die (_ ("cannot rename the current branch while not on any." ));
@@ -585,7 +586,13 @@ static void rename_branch(const char *oldname, const char *newname, int force)
585586 die (_ ("Invalid branch name: '%s'" ), oldname );
586587 }
587588
588- validate_new_branchname (newname , & newref , force , 0 );
589+ /*
590+ * A command like "git branch -M currentbranch currentbranch" cannot
591+ * cause the worktree to become inconsistent with HEAD, so allow it.
592+ */
593+ clobber_head_ok = !strcmp (oldname , newname );
594+
595+ validate_new_branchname (newname , & newref , force , clobber_head_ok );
589596
590597 strbuf_addf (& logmsg , "Branch: renamed %s to %s" ,
591598 oldref .buf , newref .buf );
@@ -784,7 +791,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
784791 if (kinds != REF_LOCAL_BRANCH )
785792 die (_ ("-a and -r options to 'git branch' do not make sense with a branch name" ));
786793 create_branch (head , argv [0 ], (argc == 2 ) ? argv [1 ] : head ,
787- force_create , reflog , track );
794+ force_create , reflog , 0 , track );
788795 } else
789796 usage_with_options (builtin_branch_usage , options );
790797
0 commit comments