Skip to content

Commit cfaff3a

Browse files
dschogitster
authored andcommitted
branch -m: allow renaming a yet-unborn branch
In one of the next commits, we would like to give users some advice regarding the initial branch name, and how to modify it. To that end, it would be good if `git branch -m <name>` worked in a freshly initialized repository without any commits. Let's make it so. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1296cbe commit cfaff3a

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

builtin/branch.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,9 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
538538
strbuf_addf(&logmsg, "Branch: renamed %s to %s",
539539
oldref.buf, newref.buf);
540540

541-
if (!copy && rename_ref(oldref.buf, newref.buf, logmsg.buf))
541+
if (!copy &&
542+
(!head || strcmp(oldname, head) || !is_null_oid(&head_oid)) &&
543+
rename_ref(oldref.buf, newref.buf, logmsg.buf))
542544
die(_("Branch rename failed"));
543545
if (copy && copy_existing_ref(oldref.buf, newref.buf, logmsg.buf))
544546
die(_("Branch copy failed"));

t/t0001-init.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,4 +571,12 @@ test_expect_success 'invalid default branch name' '
571571
test_i18ngrep "invalid branch name" err
572572
'
573573

574+
test_expect_success 'branch -m with the initial branch' '
575+
git init rename-initial &&
576+
git -C rename-initial branch -m renamed &&
577+
test renamed = $(git -C rename-initial symbolic-ref --short HEAD) &&
578+
git -C rename-initial branch -m renamed again &&
579+
test again = $(git -C rename-initial symbolic-ref --short HEAD)
580+
'
581+
574582
test_done

0 commit comments

Comments
 (0)