Update: You can now rename it directly from GitHub ๐.
Check it out: Renaming a branch on GitHub!
Original Post:
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 master
to 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.
Step 1: Select Your Repository
- Navigate to the main page of your repository on GitHub
- Copy the URL provided
Step 2: Clone the Repository to Your Local Machine
- Open your terminal
- Change the current working directory to the desired location where you want to save the project.
- Type
git clone
, followed by the URL you copied earlier.
Step 3: Modify the Branch Name
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 main
branch.
Step 4: Remove the โmasterโ Branch
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
Step 5: Celebrate!
You've done a fantastic job! ๐
Top comments (0)