To rename your Git master branch to main, you must do the following steps:
-
Navigate to your repository in the command line and issue the following commands: -
git branch -m master main-git push -u origin main -
Change your default branch on GitHub by going to your GitHub repository in your browser, and navigate to
Settings > Branchesand click on the dropdown and switch frommastertomainand clickUpdate(this will only show if you have two or more branches). Themainbranch is now your default branch. -
Update the tracking of the branch from your command line with the following command: -
git branch -u origin/main main -
Delete the old
masterbranch by going to your GitHub repository in your browser, navigate to your branches page, and click theDelete this branchbutton (the red trash bin icon). Yourmasterbranch is now gone.
If someone has a local clone of your repository, they can update their locals by following these steps:
- Got to the master branch:
git checkout master - Rename master to main locally:
git branch -m master main - Get the latest commits from the server:
git fetch - Remove the link to origin/master:
git branch --unset-upstream - Add a link to origin/main:
git branch -u origin/main - Update the default branch to be origin/main:
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main - 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!
Honestly, I didn't even know you could do that. But I think you can manually rename the branch by cloning the gist to your local machine and pushing a new branch name:
git clone https://gist.github.com/<id>.gitgit branch -m master maingit push -u origin maingit push origin --delete masterYou can get the
https://gist.github.com/<id>.gitfrom the top-right part of the gist page, where it saysEmbed. You click on it and selectClone via SSH.PS: These are AI instructions, I'm not sure that they work, I haven't tried them!