Skip to content

Instantly share code, notes, and snippets.

@jcouball
Last active June 6, 2025 20:40
Show Gist options
  • Select an option

  • Save jcouball/580a10e395f7fdfaaa4297bbe816cc7d to your computer and use it in GitHub Desktop.

Select an option

Save jcouball/580a10e395f7fdfaaa4297bbe816cc7d to your computer and use it in GitHub Desktop.

Default Branch Name Change

It's becoming increasingly common for Git repositories to rename their default branch from master to main. If you've cloned or forked a repository that has made this change, you'll need to update your local copy to reflect it. Here are the steps to do so.

For a Cloned Repository

If you have NO local changes

If your local master branch has no new commits you need to keep, the process is straightforward.

  1. Fetch the latest changes from the remote repository:

    git fetch origin
  2. Rename your local master branch to main:

    git branch -m master main
  3. Set your local main branch to track the remote main branch:

    git branch -u origin/main
  4. (Optional) Clean up the obsolete remote-tracking branch for master:

    git remote prune origin

If you HAVE local changes

If you have local commits in your master branch that you want to preserve, you'll need to rebase your work onto the new main branch.

  1. Rename your local master branch to something temporary to safeguard your work:

    git branch -m master my-work
  2. Fetch the latest changes from the remote repository to get the new main branch:

    git fetch origin
  3. Check out the new main branch and ensure it's tracking the remote main branch:

    git checkout main
    git branch -u origin/main
  4. Rebase your work from the temporary branch onto the main branch:

    git rebase my-work
  5. Once you've confirmed your changes are on the main branch, delete the temporary branch:

    git branch -d my-work

For a Forked Repository

If you have forked the repository, you'll need to update both your local clone and your fork on GitHub (or your preferred Git hosting platform).

  1. In your local repository, add the original repository as an "upstream" remote:

    git remote add upstream <URL of the original repository>
  2. Fetch the changes from the upstream repository:

    git fetch upstream
  3. Switch to your local master branch:

    git checkout master
  4. Rename your local master branch to main:

    git branch -m master main
  5. Rebase your local main branch with the upstream main branch:

    git rebase upstream/main
  6. Push the new main branch to your fork on GitHub (origin):

    git push -u origin main
  7. Delete the master branch on your forked repository on GitHub. This is done through the GitHub web interface in your fork's settings under the "Branches" section.

  8. Update your fork's default branch on GitHub to be main. This can also be done in your fork's settings under the "Branches" section.

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