Skip to content

Instantly share code, notes, and snippets.

@danieldogeanu
Last active April 24, 2024 17:19
Show Gist options
  • Star 64 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save danieldogeanu/739f88ea5312aaa23180e162e3ae89ab to your computer and use it in GitHub Desktop.
Save danieldogeanu/739f88ea5312aaa23180e162e3ae89ab to your computer and use it in GitHub Desktop.
How to rename your Git master branch to main.

To rename your Git master branch to main, you must do the following steps:

  1. Navigate to your repository in the command line and issue the following commands: - git branch -m master main - git push -u origin main

  2. Change your default branch on GitHub by going to your GitHub repository in your browser, and navigate to Settings > Branches and click on the dropdown and switch from master to main and click Update (this will only show if you have two or more branches). The main branch is now your default branch.

  3. Update the tracking of the branch from your command line with the following command: - git branch -u origin/main main

  4. Delete the old master branch by going to your GitHub repository in your browser, navigate to your branches page, and click the Delete this branch button (the red trash bin icon). Your master branch is now gone.

If someone has a local clone of your repository, they can update their locals by following these steps:

  1. Got to the master branch: git checkout master
  2. Rename master to main locally: git branch -m master main
  3. Get the latest commits from the server: git fetch
  4. Remove the link to origin/master: git branch --unset-upstream
  5. Add a link to origin/main: git branch -u origin/main
  6. Update the default branch to be origin/main: git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main
  7. Delete origin/master completely: git branch --remotes -d origin/master

Thanks Scott Hanselman for the instructions, you can read the full article here: Easily rename your Git default branch from master to main

If this was useful, you can buy me a coffee here. Thank you!

@roskyz
Copy link

roskyz commented Jul 13, 2023

Delete the old master branch by going to your GitHub

step 4 can be done by simply executing the following command: git push --delete origin master

@iambumblehead
Copy link

github settings now have an input field to rename the primary branch; using "main" as the new name accomplishes the task in one easy step.

@danieldogeanu
Copy link
Author

@iambumblehead Thanks for the tip! Good to know!

@cmjordan42
Copy link

github settings now have an input field to rename the primary branch; using "main" as the new name accomplishes the task in one easy step.

This doesn't address existing repos with existing local checkouts. This arbitrary change by github from master to main is killing me, I have no idea which repos have which name and am constantly using master then if it fails trying main.

I can't bring myself to waste hours right now to change this for all existing repos :'(

@iambumblehead
Copy link

@cmjordan42 having experienced same situation at my job about a year ago when gitlab started using "main" as default branch, what I did and recommend doing is, spend an afternoon or a day moving everything over to "main" and update your pipelines and specs to use main everywhere. Things will be nice and easy for the next person who creates a repo or updates a spec.

This arbitrary change by github from master to main is killing me

Agree with the angst of this sentiment. On the bright side, these services from github are mostly free and have beein serving users well for many years. Respectfully, maybe checkout codeberg.org and sr.ht :)

@cmjordan42
Copy link

@iambumblehead Indeed, tough to beat github on services in my experience, both for free and for enterprise. But I think the one thing that drives developers nuts more than anything is someone making a breaking change which creates work for the sake of creating work. At some point, I am sure I will go set all the olds repos to main or the new repos to master, but while I waste those precious moments of my life, I will be choking a voodoo doll of whichever person at github thought this was a worthwhile change to impose on the entire developer world. :)

@kanelv
Copy link

kanelv commented Sep 24, 2023

@iambumblehead Thanks for the tip! Good to know!

@bitsondatadev
Copy link

This arbitrary change by github from master to main is killing me

The change comes from terminology concerns related to the Black Lives Matter movement so it's not arbitrary. I agree it isn't pleasant, but if it makes even a few BIPOC developers feel more accepted, I'd say it's the right move.

@cmjordan42
Copy link

@bitsondatadev Interesting, thanks for the reference. The More You Know! 🌈

There is no 'slave' or subordinate in git (as opposed to RAID, distributed systems, etc.) so not sure why that was the impetus for Github, but oh well. I wish the original git whitepapers had happened to name it main back then... it IS a way more concise term for its function. And as the old saying goes: a rose is a rose by any other name except when they break your code ;)

@bitsondatadev
Copy link

Yeah, it's a small price to pay and GitHub has handled everything server-side, but I get it, it still isn't fun. When technology meets humans it isn't always optimal from a technological perspective, but without kindness and humanity what's the point of technology?

@angles-n-daemons
Copy link

Hi, going through this recently - the default branch configuration was in Settings > General

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