path: root/builtin/branch.c
diff options
authorJonathan Nieder <>2011-11-26 02:30:02 (GMT)
committerJunio C Hamano <>2011-11-28 19:40:46 (GMT)
commit3f59481e338c2d3167b9654a4289a9d1201f7944 (patch)
tree04944df5313a8383faec47b1ad5d9b0426db2841 /builtin/branch.c
parent017d1e134545db0d162908f3538077eaa1f34fb6 (diff)
branch: allow a no-op "branch -M <current-branch> HEAD"
Overwriting the current branch with a different commit is forbidden, as it will make the status recorded in the index and the working tree out of sync with respect to the HEAD. There however is no reason to forbid it if the current branch is renamed to itself, which admittedly is something only an insane user would do, but is handy for scripts. Test script is by Conrad Irwin. Reported-by: Soeren Sonnenburg <> Reported-by: Josh Chia (谢任中) Signed-off-by: Jonathan Nieder <> Signed-off-by: Conrad Irwin <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'builtin/branch.c')
1 files changed, 8 insertions, 1 deletions
diff --git a/builtin/branch.c b/builtin/branch.c
index 51ca6a0..24f33b2 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -568,6 +568,7 @@ static void rename_branch(const char *oldname, const char *newname, int force)
unsigned char sha1[20];
struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
int recovery = 0;
+ int clobber_head_ok;
if (!oldname)
die(_("cannot rename the current branch while not on any."));
@@ -583,7 +584,13 @@ static void rename_branch(const char *oldname, const char *newname, int force)
die(_("Invalid branch name: '%s'"), oldname);
- validate_new_branchname(newname, &newref, force, 0);
+ /*
+ * A command like "git branch -M currentbranch currentbranch" cannot
+ * cause the worktree to become inconsistent with HEAD, so allow it.
+ */
+ clobber_head_ok = !strcmp(oldname, newname);
+ validate_new_branchname(newname, &newref, force, clobber_head_ok);
strbuf_addf(&logmsg, "Branch: renamed %s to %s",
oldref.buf, newref.buf);