0
0
Gitdevops~5 mins

Default branch name configuration in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you create a new Git repository, it automatically sets a default branch name. This default name can be changed to match your team's preferences or standards. Configuring the default branch name helps keep your projects consistent and clear.
When starting a new project and you want the main branch to be named something other than 'master'.
When your team has agreed to use 'main' instead of 'master' for inclusivity and clarity.
When setting up a new Git environment and you want all new repositories to use a specific default branch name.
When automating repository creation and you want to ensure the default branch name is consistent.
When migrating old repositories and you want to align the default branch name with new naming conventions.
Commands
This command sets the default branch name to 'main' for all new Git repositories you create on your computer. It changes the global Git configuration.
Terminal
git config --global init.defaultBranch main
Expected OutputExpected
No output (command runs silently)
--global - Applies the setting for the current user across all repositories.
This creates a new Git repository named 'my-new-repo' using the default branch name you set earlier, which is 'main'.
Terminal
git init my-new-repo
Expected OutputExpected
Initialized empty Git repository in /home/user/my-new-repo/.git/
Change directory into the new repository folder to start working inside it.
Terminal
cd my-new-repo
Expected OutputExpected
No output (command runs silently)
This command shows the current branches in the repository. You will see 'main' as the default branch name.
Terminal
git branch
Expected OutputExpected
* main
Key Concept

If you remember nothing else from this pattern, remember: setting 'init.defaultBranch' changes the default branch name for all new Git repositories you create.

Common Mistakes
Not using the --global flag when setting the default branch name.
The setting will only apply to the current repository, not globally for all new repositories.
Always include --global to set the default branch name for all new repositories on your machine.
Expecting the default branch name to change in existing repositories after setting init.defaultBranch.
This setting only affects new repositories created after the change, not existing ones.
Rename the branch manually in existing repositories if needed.
Summary
Set the default branch name globally using 'git config --global init.defaultBranch main'.
Create a new repository with 'git init my-new-repo' to see the new default branch name in action.
Verify the default branch name with 'git branch' inside the new repository.