Skip to content

Instantly share code, notes, and snippets.

@pertsevds
Forked from clarkphp/git-use-main.md
Created October 28, 2022 15:03
Show Gist options
  • Save pertsevds/177e8896227458c6e1ae44b84c030aab to your computer and use it in GitHub Desktop.
Save pertsevds/177e8896227458c6e1ae44b84c030aab 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