The steps to change the git default branch from the master

Step 1: Rename the Local Branch

The first step is to rename the master branch to main in your local repository. This command will rename the master branch to main in your local repository.

git branch -m master main
  • -m: This option stands for “move” or “rename”.
  • master: The current name of the branch.
  • main: The new name for the branch.

Step 2: Push the Renamed Branch to the Remote Repository

After renaming the branch locally, you need to push the renamed branch to your remote repository. This command will push the main branch to your remote repository and set it as the default tracking branch.

git push -u origin main
  • push: This command uploads your local changes to the remote repository.
  • -u: This option sets the upstream for the branch, which means it will set the remote main branch as the default tracking branch for your local main branch.
  • origin: The default name for the remote repository.
  • main: The new name for the branch.

Step 3: Update the Default Branch in the Remote Repository

Next, you need to change the default branch in your remote repository settings. This step depends on the hosting service you are using (e.g., GitHub, GitLab, Bitbucket).

GitHub

  • Go to your repository on GitHub.
  • Click on the “Settings” tab.
  • In the left sidebar, click on “Branches”.
  • In the “Default branch” section, click on the pencil icon to edit.
  • Select main from the dropdown menu and click “Update”.
  • Confirm the update by reading the instructions and clicking the appropriate buttons.

GitLab

  • Go to your repository on GitLab.
  • Click on the “Settings” menu on the left sidebar and select “Repository”.
  • In the “Default branch” section, click the “Expand” button.
  • Select main from the dropdown menu.
  • Click “Save changes”.

Bitbucket

  • Go to your repository on Bitbucket.
  • Click on the “Settings” tab.
  • In the left sidebar, click on “Repository details”.
  • In the “Main branch” section, select main from the dropdown menu.
  • Click “Save”.

Step 4: Delete the Old master Branch

After you have changed the default branch in your remote repository, you can delete the old master branch. This command will delete the master branch from your remote repository.

git push origin --delete master
  • push: This command uploads your local changes to the remote repository.
  • origin: The default name for the remote repository.
  • --delete: This option tells Git to delete the branch.
  • master: The name of the branch to be deleted.

Step 5: Update Local Repositories

If other developers are working on the same repository, they will need to update their local copies to reflect the changes. These commands will update the local repository to track the new main branch and set it as the default branch.

git fetch origin
git branch -u origin/main main
git remote set-head origin -a
  • fetch origin: This command downloads objects and refs from another repository.
  • branch -u origin/main main: This command sets the upstream branch for the local main branch.
  • remote set-head origin -a: This command updates the remote-tracking branch.

How to Change Git Default Branch From Master?

Changing the default branch in Git from master to main has become a common practice in many development teams. This shift is part of a broader movement towards more inclusive and descriptive terminology in software development. This article will guide you through the steps required to change your default branch from master to main, covering both local repositories and remote repositories (like those hosted on GitHub or GitLab).

Similar Reads

Why Change the Default Branch?

Changing the default branch name can be beneficial for several reasons:...

The steps to change the git default branch from the master

Step 1: Rename the Local Branch...

Conclusion

Changing the default branch in Git from master to main involves several steps, including renaming the local branch, pushing the changes to the remote repository, updating the default branch settings on your hosting service, and deleting the old branch. By following these steps, you can ensure a smooth transition to the new branch name while keeping your repository organized and up-to-date....