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.
If your local master branch has no new commits you need to keep, the process is straightforward.
-
Fetch the latest changes from the remote repository:
git fetch origin
-
Rename your local
masterbranch tomain:git branch -m master main
-
Set your local
mainbranch to track the remotemainbranch:git branch -u origin/main
-
(Optional) Clean up the obsolete remote-tracking branch for
master:git remote prune origin
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.
-
Rename your local
masterbranch to something temporary to safeguard your work:git branch -m master my-work
-
Fetch the latest changes from the remote repository to get the new
mainbranch:git fetch origin
-
Check out the new
mainbranch and ensure it's tracking the remotemainbranch:git checkout main git branch -u origin/main
-
Rebase your work from the temporary branch onto the
mainbranch:git rebase my-work
-
Once you've confirmed your changes are on the
mainbranch, delete the temporary branch:git branch -d my-work
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).
-
In your local repository, add the original repository as an "upstream" remote:
git remote add upstream <URL of the original repository>
-
Fetch the changes from the upstream repository:
git fetch upstream
-
Switch to your local
masterbranch:git checkout master
-
Rename your local
masterbranch tomain:git branch -m master main
-
Rebase your local
mainbranch with the upstreammainbranch:git rebase upstream/main
-
Push the new
mainbranch to your fork on GitHub (origin):git push -u origin main
-
Delete the
masterbranch on your forked repository on GitHub. This is done through the GitHub web interface in your fork's settings under the "Branches" section. -
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.