How can I create a Git repository with the default branch name other than master?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Modern Git makes it straightforward to create repositories whose initial branch is not master. The two main approaches are to set a global default with init.defaultBranch or to choose the branch name explicitly when running git init.
Set the Global Default for New Repositories
If you want all newly created repositories on your machine to start with main or another branch name, configure Git globally.
After that, new repositories created with git init will start with main instead of master.
You can verify the setting with:
This is the cleanest approach if you want one consistent default across all future repositories.
Choose the Initial Branch for One Repository
If you only want to override the default for a single new repository, pass the branch name directly to git init.
Or:
This is useful when one project should start on a different branch without changing your machine-wide default.
What Happens on Older Git Versions
The global init.defaultBranch and git init -b behavior became available in Git 2.28 and later. On older Git versions, a common fallback was:
Or on newer Git with the modern command style:
That works, but it creates the repository first and then changes the branch afterward instead of naming it correctly from the start.
Renaming an Existing Default Branch
If the repository already exists and started on master, you can rename the current branch.
After that, if the repository is connected to a remote hosting service, you usually also need to:
- push the renamed branch
- update the remote's default branch setting
- update CI, deployment scripts, or documentation that still references the old name
Creating a new repository with a different initial branch is easier than renaming later, but both are manageable.
Remote Hosting Is a Separate Setting
Git itself controls the local initial branch name. Hosting providers such as GitHub, GitLab, or Bitbucket may also have their own default branch settings for newly created remote repositories.
That means you sometimes need to coordinate two levels of configuration:
- local Git default branch behavior
- repository host default branch behavior
If those do not match, the local and remote experience can feel inconsistent.
A Practical Workflow
A reliable modern workflow looks like this:
For one-off exceptions:
This keeps the default clean while still allowing project-specific branch naming when needed.
Common Pitfalls
The biggest pitfall is assuming changing the global default will rename existing repositories. It does not. It only affects repositories initialized after the setting is changed.
Another issue is forgetting that older Git installations may not support init.defaultBranch or git init -b. In those environments you need the two-step init-then-rename approach.
Developers also update the local branch name but forget to update the remote default branch or downstream tooling such as CI workflows and deployment scripts.
Finally, do not assume every project should use the same branch name blindly. Consistency helps, but shared tooling and team conventions matter too.
Summary
- Use
git config --global init.defaultBranch mainto change the default for future repositories. - Use
git init -b <name>for a one-repository override. - These features are available in modern Git releases starting with 2.28.
- Older Git versions need a two-step init and branch rename workflow.
- Changing the default for new repos does not rename existing repositories automatically.
Related reading
- How can I deal with this Git warning? "Pulling without specifying how to reconcile divergent branches is discouraged
- How can I deal with this Git warning? Pulling without specifying how to reconcile divergent branches is discouraged
- How can I delete a remote tag?
- How can I delete all of my Git stashes at once?
- How can I delete the current Git branch?
- How can I determine when a Git branch was created?
- How can I discard modified files?
- How can I easily fixup a past commit?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseInterview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.