Update: You can now rename it directly from GitHub 🎉.
Check it out: Renaming a branch on GitHub!
Since October 1, 2020, all new repositories on GitHub have defaulted to using
main as the branch name. If you created repositories before this date, your branches will be named
master. GitHub, along with the broader Git community, is gradually transitioning the default branch name from
main, a convention that's slowly being adopted across the community. In this guide, we'll walk you through the process of renaming your branch, step by step.
- Navigate to the main page of your repository on GitHub
- Copy the URL provided
- Open your terminal
- Change the current working directory to the desired location where you want to save the project.
git clone, followed by the URL you copied earlier.
While still in your terminal, navigate to the root folder of your project using
cd <your-project-folder> and execute the following command:
git branch -m master main
This command creates a new branch named
main and switches to it, with the
-m flag transferring all the commit history from
master to your new
Now it's time to remove the old branch.
Execute the following command to set your local machine to track the new branch and update your remote GitHub repository:
git push -u origin main.
Next, point the HEAD to the current branch reference with:
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main
You can now verify your tree using:
git branch -a
- Navigate to your GitHub repository, go to the Settings section, find the branches section, and switch the default branch to “main”, as shown below.
- Return to your terminal and delete the old branch both locally and remotely with:
git push origin --delete master
You've done a fantastic job! 😎