To set a new default Git branch (e.g., `main`) in your local repository, use the following command:
git branch -m master main
This command renames the current `master` branch to `main`. To set `main` as the default for future clones in the GitHub repository, you can also run:
git push -u origin main
git push origin --delete master
Understanding Default Branches
What is a Default Branch?
A default branch in a Git repository is the primary branch where development occurs. This branch typically serves as the mainline of development and is where new features and fixes are integrated. When a new clone of the repository is created, Git automatically checks out this branch. The default branch is crucial in collaborative environments, as it establishes a common starting point for all contributors. Traditionally, this branch has been named `master`, but there is a growing movement towards adopting more inclusive terminology, such as `main` or `development`.
Why Change the Default Branch?
There are several compelling reasons to change your default branch. One of the most significant is the recognition of terms that may carry historical weight. Many organizations are opting to transition from `master` to `main` for inclusivity and to reflect modern values. Additionally, as teams evolve, their preferred workflows may shift, making a change in the default branch beneficial to better align with current practices. Understanding when and why to change the default branch allows for a more effective collaboration strategy among team members.

Checking the Current Default Branch
How to Identify Your Default Branch
To figure out which branch is the current default branch in your repository, you can use the following command:
git symbolic-ref refs/remotes/origin/HEAD
This command retrieves the reference to the current HEAD of the remote, telling you which branch is currently set as the default.
Understanding Your Repositories' Branch Structure
To view all the branches in your repository, both local and remote, utilize the command:
git branch -a
This list helps you understand your repository’s structure and recognizable branches, allowing for informed decisions when setting a new default branch.

Setting a New Default Branch
Creating a New Branch
Before changing the default branch, you may want to create a new branch. This could be for a new feature, version, or just a fresh start. Here’s how to do it:
git checkout -b new-branch-name
This command creates a new branch and checks it out immediately, setting the stage for your work.
Renaming the Default Branch
If you intend to rename the old default branch, follow these steps to change it locally:
git branch -m old-branch-name new-branch-name
This command renames the specified branch, ensuring that all local references point to the new branch name.
Updating the Default Branch on Remote
Pushing the New Branch to Remote
Once the local branch is renamed, the next step is to push your changes to the remote repository. You should use:
git push origin new-branch-name
This command uploads the new branch to your remote repository, making it available for others to use.
Changing the Default Branch in Git Hosting Services
After successfully pushing the newly renamed branch, you need to set it as the default branch in your hosting service, like GitHub or GitLab. Follow these general steps:
- Navigate to the repository settings in your Git hosting platform.
- Locate the "Branches" section where you can see the current default branch.
- Select the new default branch from the dropdown list and save your changes.
For instance, in GitHub:
- Go to the repository's Settings tab.
- Click on Branches on the left sidebar.
- Change the default branch to your new branch.

Setting the Default Branch for New Clones
Updating the Repository Configuration
To ensure that any future clones of the repository default to your newly set branch, you can adjust the configuration by executing this command:
git config --global init.defaultBranch new-branch-name
By setting the global default branch, all future repositories initialized on your system will default to the specified branch name, streamlining workflow processes.

Verifying the Changes
Confirming the New Default Branch
To verify that your changes have taken effect, you can use:
git remote show origin
This will show the current branches and specified default branch in the remote repository, allowing you to confirm the modifications.
Checking on Git Hosting Platforms
After changing the default branch, you should also check your Git hosting platform. Ensure you navigate to the repository settings again to validate that your preferred branch is now set as the default.

Best Practices for Managing Default Branches
Consistency Across Teams
Changing the default branch should not be a unilateral decision. Collaboration and communication with your team are essential. Ensure everyone is informed and supportive of the change to avoid confusion and misalignment in workflows. Consider organizing a meeting or providing documentation that explains the reason behind the change and how it impacts collaboration.
Documenting Changes
Properly documenting the transition to a new default branch is key for maintaining historical context and clarity among team members. Make sure to update any relevant documents, such as your README or CONTRIBUTING guidelines, so that any newcomers or collaborators are aware of the new default branch and its purpose in your repository.

Conclusion
Changing the default branch in Git is a straightforward process, but it carries significant implications for your project's collaboration and development workflows. By following the steps outlined in this article on how to set a new default Git branch, you can align your repository practices with modern standards and foster a more inclusive environment for your team. Embrace this change, and don’t hesitate to explore further Git commands and strategies to enhance your workflow!

Additional Resources
For deeper insights and further reading on Git, check out the official Git documentation or explore tools designed to streamline Git workflows.