Change git init default branch name

PHOTO EMBED

Wed May 04 2022 16:51:18 GMT+0000 (Coordinated Universal Time)

Saved by @RLMA2

https://superuser.com/questions/1419613/change-git-init-default-branch-name

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)

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).

#############################################################################

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)

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.

#############################################################################

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 -l
$ 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.)

#############################################################################

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

#############################################################################


#############################################################################
Common setup queries
$ git config --global -l
$ git config --system -l
$ git config --local -l
$ git config --local -l

Common setting changes
$ git config --global init.defaultBranch main


#############################################################################
From https://www.git-tower.com/learn/git/faq/git-rename-master-to-main
and https://stackoverflow.com/questions/70883005/why-is-there-two-init-defaultbranch-entries-in-my-gitconfig-file

rename branch master to main - git branch -m master main ### Not Main
check if this has worked as expected:
$ git status
On branch main
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

Renaming the Remote master Branch as Well
$ git push -u origin main

remove the old "master" branch on the remote
$ git push origin --delete master

Depending on your exact setup, this might have worked and the renaming is successful. In many cases, however, you will see an error message like the following one:

To https://github.com/gittower/git-crashcourse.git
! [remote rejected]   master (refusing to delete the current branch: refs/heads/master)
error: failed to push some refs to 'https://example@github.com/gittower/git-crashcourse.git'
 Save
GitHub, like other code-hosting platforms, too, expects you to define a "default" branch - and deleting this is not allowed. Additionally, your old "master" might be set as "protected". You'll need to resolve this before you can go on. Here's how to do this in GitHub:

On Github in the project Settings => Branches=> Default branch => select main as default and commit change

Again on the remote
$ git push origin --delete master
To https://github.com/gittower/git-crashcourse.git
 - [deleted]           master
 
 What Your Teammates Have to Do
If other people on your team have local clones of the repository, they will also have to perform some steps on their end:

# Switch to the "master" branch:
$ git checkout master

# Rename it to "main":
$ git branch -m master main

# Get the latest commits (and branches!) from the remote:
$ git fetch

# Remove the existing tracking connection with "origin/master":
$ git branch --unset-upstream

# Create a new tracking connection with the new "origin/main" branch:
$ git branch -u origin/main
content_copyCOPY

https://superuser.com/questions/1419613/change-git-init-default-branch-name