Skip to content

Instantly share code, notes, and snippets.

@clarkphp
Created August 15, 2020 11:01
Show Gist options
  • Save clarkphp/43561ea739a0a31958c57f259adef2cd to your computer and use it in GitHub Desktop.
Save clarkphp/43561ea739a0a31958c57f259adef2cd to your computer and use it in GitHub Desktop.
Use default branch name of "main" instead of "master" in Git repositories

Set default branch name for new repositories to "main"

git version 2.28+

$ git config --global init.defaultBranch main

git version 2.27 or less

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

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

Change existing repository master branch name to "main"

$ git branch -m master main
$ git push -u origin main

$ # update local clones:
$ 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

Reference: https://www.hanselman.com/blog/EasilyRenameYourGitDefaultBranchFromMasterToMain.aspx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment