Skip to content

Instantly share code, notes, and snippets.

@jonlabelle
Last active September 10, 2024 07:15
Show Gist options
  • Save jonlabelle/81206faafb56714d7499efa54eb8f3ff to your computer and use it in GitHub Desktop.
Save jonlabelle/81206faafb56714d7499efa54eb8f3ff to your computer and use it in GitHub Desktop.
Change GitHub default branch from master to main.

Change GitHub default branch from master to main

5 simple steps that I tested and used to make the change in under 1 minute.

  1. Move the master branch to main

    git branch --move master main
  2. Push main to remote repo

    git push --set-upstream origin main
  3. Point HEAD to main branch

    git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main
  4. Change default branch to main on GitHub

    • Navigate in your browser to your GitHub repository.
    • From the left rail, click Settings -> Branches and change the default branch to main
  5. Delete master branch on the remote repo

    git push origin --delete master

Updating a local clone after a branch name changes

After you rename a branch in a repository on GitHub, any collaborator with a local clone of the repository will need to update the clone.

From the local clone of the repository on a computer, run the following commands to update the name of the default branch.

git branch -m OLD-BRANCH-NAME NEW-BRANCH-NAME
git fetch origin
git branch -u origin/NEW-BRANCH-NAME NEW-BRANCH-NAME
git remote set-head origin -a

Optionally, run the following command to remove tracking references to the old branch name.

git remote prune origin

Source: GitHub: Updating a local clone after a branch name changes


To initialize a new Git repository and set the default branch to main:

git init --initial-branch=main

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