Skip to content

Instantly share code, notes, and snippets.

@DeeprajPandey
Created November 11, 2023 16:54
Show Gist options
  • Save DeeprajPandey/412f4d0a732bec7059a1b14e63ba83dd to your computer and use it in GitHub Desktop.
Save DeeprajPandey/412f4d0a732bec7059a1b14e63ba83dd to your computer and use it in GitHub Desktop.
Rename default branch from master to main (and push changes to remote)

How to rename default branch in git on older repos...

Scenario: old repo, checked out locally. You discover that the default branch is master.

Follow these instructions to rename the branch - locally and on your remote copies.

Instructions

  1. Make sure you are on master
git checkout master
  1. Rename the current local branch
git branch -m master main
  1. Check if the rename worked
git status

It should specify that it's currently on branch main and it's set to track 'origin/master'. 4. Remove the upstream reference

git branch --unset-upstream
  1. Push to remote, this will create a new main on remote
git push -u origin main

It should confirm branch 'main' set up to track 'origin/main' 6. To delete master from remote, you will have to first set the newly made main as your new default (if you're using GitHub). Go to Settings -> General -> Default Branch -> Switch and select main and confirm changes. 7. Delete remote master

git push origin :master

References

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