244

I'm trying to figure out how to make git init use a different default branch name other than master for the first commit, but I can't find a git config for it or anything that'd allow me to do this (e.g. aliases only work for commands, not branch names).

Is there any way to change the default first branch name that git init sets up?

7 Answers 7

370

As of Git 2.28 (released 27th July 2020), you can now configure the name of the branch created when you init a new repository:

$ git config --global init.defaultBranch main

After setting this variable, running git init will produce a repository whose initial branch is main:

$ git init
Initialised empty Git repository in /home/thomas/test-git-repo/.git/
$ git status
On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)

Release notes: https://lore.kernel.org/git/[email protected]/

9
  • 1
    @RCross I'm not totally sure I understand what you mean - do you mean if you are running the Github software on your server, would this command be sufficient to mean that users of that server would get new repos with the default name? If so, I certainly don't know. I was under the impression GH used a bespoke fork of Git anyway. If you mean can a user of github.com set the default name of new repositories, I would assume the answer is no - this command affects only local calls of git init AFAICT.
    – T. Kiley
    Commented Aug 19, 2020 at 12:39
  • 4
    as of git 2.25.1 it doesn't work
    – Sayan Dey
    Commented Jan 5, 2021 at 7:11
  • 5
    @SayanDey Yeah unfortunately, as stated in the answer, this feature was introduced in 2.28, so this won't work for older versions. Consider checking this answer for a workaround.
    – T. Kiley
    Commented Jan 5, 2021 at 13:46
  • 1
    @EmadBaqeri This doesn't affect existing repositories (you'll have to rename those manually). Also, if you're using an old version of git this won't work (but won't give an error as git config can be used to setup arbitrary key/value pairs.
    – T. Kiley
    Commented Oct 29, 2021 at 12:59
  • 1
    FYI Windows users should consider upgrading. The Git for Windows installer circa 2.41 prompts users if they want to change the default init branch with main as the suggested name
    – KCD
    Commented Jun 6, 2023 at 20:56
32

In Git versions prior to 2.28, HEAD is hardcoded to point to refs/heads/master.

if (create_symref("HEAD", "refs/heads/master", NULL) < 0)

So there is no config setting or option that you can pass to git init to change it to something else.

What is possible though is to change what HEAD points to right after git init with the help of git symbolic-ref:

$ git init
$ git symbolic-ref HEAD refs/heads/test

This will change HEAD to point to a (not yet existing) branch called test. Then when you create your first commit, the branch will be called test instead of master.

0
18

A simple way to change the default HEAD is to create a HEAD in the git template dir. First, configure your template dir to ~/Templates/git.git (or whatever you'd prefer):

$ git config --global init.templateDir '~/Templates/git.git'
$ cp -r /usr/share/git-core/templates ~/Templates/git.git

Then, create the file HEAD in the template dir:

$ echo 'ref: refs/heads/default' > ~/Templates/git.git/HEAD

And you're good to go! Whenever you run git init, you'll now get the message:

$ git init
Reinitialized existing Git repository in [...]

For some reason, git decides whether to use this message based on the presence of the HEAD file in .git, rather than relying on whether or not .git had to be created. However, it really doesn't matter what message it shows you. From the git-init man page:

Running git init in an existing repository is safe. It will not overwrite things that are already there. The primary reason for rerunning git init is to pick up newly added templates (or to move the repository to another place if --separate-git-dir is given).

That is to say, git init is guaranteed not to overwrite the HEAD you put in the template, and it won't use the template's HEAD to overwrite an existing HEAD either. Since this is explicitly documented, you can rely on it.

Additionally, it also says:

Files and directories in the template directory whose name do not start with a dot will be copied to the $GIT_DIR after it is created.

Which means you can also rely on the template being copied immediately after the creation of .git, and not at a later point.

(Of course, this is my personal interpretation of the manual. It's entirely possible that the git developers will disagree.)

1
  • 1
    This is a great solution on a 'controlled' laptop where I do not want to install a later version of git!
    – sage
    Commented Apr 30, 2021 at 17:17
15

One way to set your default branch is to edit your ~/.gitconfig file. Add the following lines:

[init]
  defaultBranch = main

Now when you run git init, main will be your default branch. This is similar to running git config --global init.defaultBranch main as mentioned by @t-kiley.

4
  • 2
    Manual git config file manipulation seems not very good idea when there are simple commands which give exactly the same result in a much more consistent way. Commented Jul 19, 2022 at 8:17
  • 1
    @VictorYarema can you elaborate? My understanding is that this is the very reason for a config file. I've been maintaining one for years and it makes it easy to bootstrap a new computer without needing to remember commands. It can also be shared with a dev team for consistent git setups.
    – wsams
    Commented Jul 21, 2022 at 0:28
  • I mean that I would recommend not manipulations with config files directly. The way I share the config is just git config --global user.useConfigOnly true && git config --global core.autocrlf false && .... One can also do git config --global --unset ... if needed. Commented Jul 21, 2022 at 8:07
  • 1
    @VictorYarema Doesn't matter in case of Git. It will ignore any options it can't understand and the commands also let you set invalid options as they don't do any checks. They are just an additional way to access the configuration for users and scripts. Official documentation says: "Git’s configuration files are plain-text, so you can also set these values by manually editing the file and inserting the correct syntax."
    – Bachsau
    Commented Feb 19, 2023 at 12:28
3

As mentioned HEAD is hardcoded to point to master. However, you could create a shell alias, to make git init do what you like. If you are using bash as your shell, put something like this in your .bashrc:

function git_init_fnc () {
  default_branch="main"
  if [[ $1 == "init" ]] && [[ $# -eq 1 ]];then
      git init
      if [[ ! -z $(git branch -a | grep $default_branch) ]]; then
          git checkout "$default_branch"
      else
          git checkout -b "$default_branch"
      fi
  else
      /usr/bin/git "$@"
  fi
}

alias "git"=git_init_fnc

This will replace the command git with a function. This function will make the command git run exactly the same, unless you are calling git init without any other arguments. When you call git init it will init the repository. Next it will check to see if the branch "daddy" already exists. If it does, it will check out that branch, otherwise it will create the branch and move you to it.

3

Most of the answers to this thread work for some git versions, but no answer worked for all of the versions I'm working with (git versions 2.7, 2.25 and 2.30).

But thanks to all of your answers, I was able to mix your shared ideas and I finally found a way to support all versions with one setting:

[Edit: Turned out, that my observation was wrong. It didn't work, because of my special setup.
If you have problems with the above solutions, here's another approach (tested for git versions 2.7, 2.25 and 2.30).]

init.defaultBranch combined with a wrapper function (only for git versions, that do not support init.defaultBranch, which is queried once, at the beginning of the session).

The wrapped function issues a git checkout to the configured default branch, directly after an init.

.gitconfig

…
   [init]
    # Usually only works for git >= 2.28.
    # Thus, these dotfiles provide a git wrapper that performs extra
    # action after git init. The git wrapper takes this setting
    # into account, so that it even works for git < 2.28.
    defaultBranch = "main"
…

bash aliases

#!/usr/bin/env bash

main() {
  wrap_git_if_necessary
}

# if the current git version does not support a custom
# default branch (init.defaultBranch, e.g. main),
# then we wrap it in a function that performs
# extra action on git init.
wrap_git_if_necessary() {
  if ! supports_default_branch &>/dev/null; then
    alias git=git_wrapper
  fi
}

supports_default_branch() {
  command git help --config | grep "init.defaultBranch"
}

# wrap git to make main the default branch on init.
# Is required for git versions < 2.28, which do
# not support init.defaultBranch natively.
git_wrapper() {
  local defaultBranch
  if command git "$@"; then
    if [[ "$1" == "init" ]]; then
      defaultBranch=$(command git config init.defaultBranch || echo "main")
      command git checkout -b "${defaultBranch}"
    fi
  fi
}

main "$@"

2
  • That's false. The chosen answer works on all versions.
    – SoniEx2
    Commented Dec 24, 2021 at 21:06
  • Actually, you're right. I tried to reproduce the error and noticed, that it didn't work for me, because I used a link to a .dotfile HEAD file with the same contents. This approach worked well with git 2.7.4, but it didn't work with git 2.25.1 (fresh Mint 20.2 installation). The new approach works for all versions in my setup. It's one config for all and it doesn't output "Reinitialized existing git repository" when I init a new one. So, your approach (the chosen answer) actually works for all versions, but I'll keep mine. Thanks for the clarification. Commented Dec 26, 2021 at 5:33
1

I found this question via search. I realize I'm answering an old question, but to others who may get here, GitHub has a page on configuring the primary branch name for any repositories created there, as well as instructions for changing the primary branch name:

Renaming the default branch from master

5
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Feb 13, 2022 at 10:18
  • 1
    Answering an old question is fine if it's lacking a good answer. However, in this case, there are several, and they all solve the problem, while your answer contributes nothing useful.
    – Bachsau
    Commented Feb 19, 2023 at 12:36
  • No other responses addressed branch names on GitHub. Please read before commenting.
    – guyr
    Commented Feb 21, 2023 at 7:33
  • @guyr, the question didn't ask about GitHub. Commented Nov 10, 2023 at 17:38
  • @NathanMusoke I realize, but I thought I'd add this info in case someone is trying to do the same thing on GitHub. Just trying to be helpful.
    – guyr
    Commented Nov 12, 2023 at 5:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .