Skip to content

Instantly share code, notes, and snippets.

@plembo
Last active December 29, 2020 21:01
Show Gist options
  • Save plembo/57c18254d6d9a8301e8ee3c83da089c8 to your computer and use it in GitHub Desktop.
Save plembo/57c18254d6d9a8301e8ee3c83da089c8 to your computer and use it in GitHub Desktop.
Set default git branch name to main

Setting default branch name to main

GitHub is now naming the default branch for new repositories "main" instead of "master", while the latter is still the default for the git command-line tool. With git v2.28, you can configure git to set a name other than "master" for the default branch. Of course, once that's done there's still the "little" matter of making the change to all your repos. GitHub says they're going to do this in the background starting in January 2021, and in fact advise against making the change yourself.

Make git init use "main"

For all future repos created locally:

$ git config --global init.defaultBranch main

Rename existing "master" to "main"

This requires multiple steps:

  1. Move master to main:
$ git branch -m master main
  1. Push main to remote
$ git push -u origin main
  1. Change default to main on GitHub: In the web gui, go to Settings... Branches... Default branch and select "main" from the drop-down.
  2. Delete master on remote repo (this can also be done from the web gui)
$ git push origin --delete master
  1. Point head to main (there's another way to do this, see note below)
$ git remote set-head origin --auto

There may still be some cruft from master in your local repo. To try to clear this out:

$ git branch --unset-upstream
$ git branch -u origin/main

While my remotes all looked clean, some of my local working copies continued to show artifacts from "master" (more than a few from gitlab, which has been their home for a time). The quick solution was to delete from disk and re-clone from remote.

Resources:

Stephen M. Mortimer. 5 steps to chane GitHub default branch from master to main. R-bloggers. July 23, 2020. Retrieved December 29, 2020.

GitHub. Renaming the default branch from master. July 21, 2020. Last updated December 17, 2020. Retrieved December 29, 2020.

Dirk Avery. Renaming a master Branch to main Step-by-Step. FAUN. July 20, 2020. Retrieved December 29, 2020.

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