How to change the default branch name for `git init`

When a new Git repository is initialized using git init, the default name of the main branch that is created is “master” (likely based on the terminology used in BitKeeper, which Git was created to replace).

Update Nov 2020: This is now the default in Git.

For the past 20+ years, the usage of “master/slave” terminology has been criticized as needlessly reproducing racist terminology of slavery, and in the course of 2020, this discussion has finally broadly reached Git as well, with calls to rename the default branch to “main” or “trunk” instead (previously, this had been proposed for specific projects, e.g. GNOME in 2019).

Petr Baudis, who picked the names way back in 2005, weighed in to state that they wish they had picked “main” instead. Using “main” instead of “master” has the benefit that it autocompletes with “m” and “ma” as well, and is also the term GitHub (who still collaborate with ICE) are switching to.

Currently, the branch name a new repository is initialized with is hard-coded in Git, so there are two options to change the default (to “main”, in the following examples):

  1. Changing the HEAD pointer before the first commit, using the symbolic-ref subcommand: git symbolic-ref HEAD refs/head/main or manually overwriting the .git/HEAD file: echo 'refs: refs/head/main' >.git/HEAD;
  2. Putting this change into a git template, by copying the default git template: cp -r /usr/share/git-core/templates ~/git/template, adding the ref pointer: echo 'ref: refs/heads/main' >~/git/template/HEAD, and passing that directory to git init in the --template= option (or setting init.templateDir).

If you want to easily change the main branch name of existing repositories, you can just run git branch -m master main && git push -u origin main. Remember to adapt any settings or scripts that might refer to branches by name, such as CI/CD, “protected branches”, etc.!

Leave a Reply

Your email address will not be published. Required fields are marked *