When initializing a project with Git for local management, a master
branch is created. Since GitHub often promotes the use of "main" as the branch name, let's explore how to rename the branch.
Changing the Branch Name with git
Commands
We will change the branch name from "master" to "main."
Local Repository
-
Initialize Git.
git init
-
Rename the branch.
git branch -m master main
Remote Repository
- Clone the remote repository.
git clone repository-url
- Navigate to the remote repository.
cd repository
- Rename the branch.
git branch -m master main
- Push the changes to the remote repository.
git push -u origin main
- Delete the old branch from the remote repository.
git push origin --delete master
Renaming the Branch Directly on the Remote Repository
On GitHub, you can rename the branch directly on the web without going through the above steps.
- Go to the page at https://github.com/owner-name/branch-name/branches.
- Click on the hamburger menu and select
Rename branch
.
Changing the Default Branch Name on Initialization
To change the default branch name created during Git initialization to "main," use the following command:
git config --global init.defaultBranch main