Git: Rename “master” to “main”

More and more places are starting to rename their “master” branch to “main”. Here is a short description on one way to do that

1. We start with pulling/commiting all our work so that our Git tree is clean

$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

2. Now we rename our local branch

niklas@HOME MINGW64 /c/git/my-project (master)
$ git branch -m master main

niklas@HOME MINGW64 /c/git/my-project (main)

3. Now we create the “main” branch on our remote

$ git push -u origin main
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: To create a merge request for main, visit:
remote:   https://git.mycompany.com/nb1-team/my-project/-/merge_requests/new?merge_request%5Bsource_branch%5D=main
remote:
To https://git.mycompany.com/nb1-team/my-project.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

4. and lastly we remove the “master” branch to prevent any misstakes commiting to it in the future

$ git push origin --delete master
To https://git.mycompany.com/nb1-team/my-project.git
 - [deleted]         master

NOTE! If you have pipeline scripts that depended on “master” before you need to update them to use “main” now
NOTE 2! If your project used “master” as “Default branch” you need to change that to “main” now

Tested on Windows 10 and GitBash v3.5.2

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">

This site uses Akismet to reduce spam. Learn how your comment data is processed.