Skip to content

Instantly share code, notes, and snippets.

@akash02-das
Last active July 28, 2023 22:04
Show Gist options
  • Save akash02-das/1a4e18d68ee85eb52387c9617c994da1 to your computer and use it in GitHub Desktop.
Save akash02-das/1a4e18d68ee85eb52387c9617c994da1 to your computer and use it in GitHub Desktop.
Renaming the Local/Remote master Branch to main

Renaming the Local/Remote master Branch to main

  • The first step is to rename the "master" branch in your local Git repositories:

    $ git branch -m master main
  • Let's quickly check if this has worked as expected:

    $ git status
    On branch main
    Your branch is up to date with 'origin/master'.
    
    nothing to commit, working tree clean
    

Renaming the Remote master Branch as Well

  • Make sure your current local HEAD branch is still "main" when executing the following command:

    $ git push -u origin main
  • We now have a new branch on the remote named "main". Let's go on and remove the old "master" branch on the remote:

    $ git push origin --delete master
  • Depending on your exact setup, this might have worked and the renaming is successful. In many cases, however, you will see an error message like the following one:

    To https://github.com/example/git-example.git
    ! [remote rejected]   master (refusing to delete the current branch: refs/heads/master)
    error: failed to push some refs to 'https://[email protected]/example/git-example.git'
    
  • GitHub, like other code-hosting platforms, too, expects you to define a "default" branch - and deleting this is not allowed. Additionally, your old "master" might be set as "protected". You'll need to resolve this before you can go on. Here's how to do this in GitHub: See this video

  • If you try again now, deleting "master" from the remote repository should be successful:

    $ git push origin --delete master
    To https://github.com/example/git-example.git
    - [deleted]           master
    

That’s it. You have successfully renamed the master branch to main. 🥳

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