Make main the default git branch

Submitted by michael on Tue, 06/23/2020 - 13:51

New Repository

Git will use a user-defined template when starting a new git repository, so configure that template to use "main" instead of the default.

git config --global init.templateDir '~/.git-template'
cp -r /usr/local/share/git-core/templates ~/.git-template
echo 'ref: refs/heads/main' > ~/.git-template/HEAD

From that point on git init will use the template with the HEAD pointing to main.

Existing Repository

Git can move/rename a branch quite easily. But once you use the commands below you also need to manually change the default branch in GitHub's settings. And if you have CI/CD set up you also need to manually change that as well.

git branch -m master main
git push -u origin main
git push origin --delete master
git remote set-head origin -a

Local Clone

This one isn't as easy. From the default branch you rename that default to main, fetch the renamed branch, set the branch to point to the right upstream, and then shift the HEAD pointer. Sigh. And once you use the commands below you also need to manually change the default branch in GitHub's settings.

git checkout master
git branch -m master main
git fetch
git branch --unset-upstream
git branch -u origin/main
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main

Tags