loading...
Cover image for Cheatsheet for moving from Master to Main

Cheatsheet for moving from Master to Main

swyx profile image shawn swyx wang πŸ‡ΈπŸ‡¬ Updated on ・2 min read

Share on Twitter

For my own reference, and anyone else interested in moving primary git branch from master to main.

I'm not interested in discussing reasons to do this here, it has been rehashed thousands of times already.

TL;DR:

Cheatsheet for Master to Main

Move Existing Projects

1. Rename branches

git branch -m master main # history unchanged
git push origin HEAD
git branch -u origin/main main

2. retarget existing PRs

You can use https://github.com/ethomson/retarget_prs to do it.

3. set default branch

Make sure you've pushed your main branch, then head to https://github.com/USERNAME/REPONAME/settings/branches - docs here

4. update local clones (if applicable)

This is handy if you are working on local forks of OSS - Thanks to https://twitter.com/xunit/status/1269881005877256192

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

Set Default For New Projects

You can set new projects created on your machine to start with main branch as well.

## Git 2.28+
git config --global init.defaultBranch main
## Git 2.27-
git config --global alias.new '!git init && git symbolic-ref HEAD refs/heads/main'

If you're on a Mac like me, you can brew upgrade git or download Git to update the version. Recent versions also include sparse-checkout, in case you needed more incentive to upgrade.

This is for new projects on your local machine - unfortunately GitHub hasn't made a new setting for setting the default main branch for new repos created on GitHub yet.

Edit: now it has! head to https://github.com/settings/repositories to set it

Alt Text

Set Bash Aliases

Lastly, you can setup bash aliases that tries main first and then master so you get to use the same alias no matter what you work with:

alias gpom="git push origin main 2> /dev/null || git push origin master"

GitHub's Plans

If the GitHub process seem rather manual, don't worry. I have hundreds of repos on GitHub. GitHub has plans to release automated tooling to help you manage this.

Plans: https://github.com/github/renaming

Posted on by:

swyx profile

shawn swyx wang πŸ‡ΈπŸ‡¬

@swyx

Infinite Builder πŸ‘·πŸ½β€β™‚οΈI help people LearnInPublic β€’ more at http://twitter.com/swyx

Discussion

markdown guide
 

Nice - I had been using the Scott Hanselman approach (which is the same that you have) which has the advantage (as far as I can see) that it sets the remote for you automatically - so I think you lose the requirement for the bash alias.

his article is here : hanselman.com/blog/EasilyRenameYou...

but I think the 'magic' is here :

git push -u origin main

which sets the upstream.

I hadn't thought about Pull Requests -

 

Thanks for writing.
Don't forget adjusting your pipelines (e.g. Github actions), in case some of them are only triggered on the former master branch :)