1

Is it possible to rename the master in git?
If that's not possible, is there a way to obtain a similar result? e.g. create a branch with a custom name (say batman), make that branch the new master and delete the old master?

To exemplify. This is what I have now:

user@machine gitprojectpath (master)
$

This is what I'd like to have

user@machine gitprojectpath (batman)
$
CAPSLOCK
  • 6,243
  • 3
  • 33
  • 56
  • `master` is not special in git. The fact that it is very often used as the main branch is pure convention. Look at [How do I rename a local Git branch?](https://stackoverflow.com/questions/6591213/how-do-i-rename-a-local-git-branch) for the renaming part. – Romain Valeri Mar 22 '19 at 11:08
  • @RomainValeri Not completely. In GitLab, the first branch (not necessarily named `master`) pushed to the repository becomes the *default branch* which has plenty of special meaning. It is the default branch checked out by clients on a clone, and it is the default merge request target. Also, it is usually *protected*. – Jonathon Reinhart Mar 22 '19 at 11:11
  • 1
    Why do you want this? – Bernhard Mar 22 '19 at 11:15
  • Definitely you can! See @JonathonReinhart's answer. @Bernhard: (The name) `master` is nothing special from a technical point of view, it's just a git convention for the "default" branch. Same applies for `origin` as "default" remote name. – andreee Mar 22 '19 at 11:21

1 Answers1

5

Yes.

First, to clarify your question.

In GitLab, the first branch (not necessarily named master) pushed to the repository becomes the default branch which has special meaning. It is the default branch checked out by clients on a clone, and it is the default merge request target. Also, it is usually protected.

When you say "rename master", I assume you mean rename the branch, and change the default branch in GitLab.

First rename the branch locally.

git checkout master
git branch -m newname

Then push it to the server

git push -u origin newname

Now go to the GitLab web UI. Under Settings > Repository, change the "default branch" to newname.

You'll also want to make sure that newname is marked protected to prevent accidental loss by a force-push.

Then delete master on GitLab from the branches page.


With that said, please don't do this. master is an extremely well-known convention and this change will be surprising to other contributors.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • 2
    _"please don't do this"_ - maybe the OP has used their master branch for releases and now wants to rename it to "release" (without branching) so that some other development branch can be used as the new master? No criticism, just an idea why one would do it... :-) does it make sense? – andreee Mar 22 '19 at 11:26
  • 7
    And here's another reason someone might want to validly rename it; https://www.zdnet.com/article/github-to-replace-master-with-alternative-term-to-avoid-slavery-references/ – d219 Jul 02 '20 at 13:12