Git
Repository
Default Branch
Version Control
Software Development

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.

Browse interview questions

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.

bash
git config --global init.defaultBranch main

After that, new repositories created with git init will start with main instead of master.

You can verify the setting with:

bash
git config --global --get init.defaultBranch

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.

bash
git init -b develop

Or:

bash
git init --initial-branch=main

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:

bash
git init
git checkout -b main

Or on newer Git with the modern command style:

bash
git init
git switch -c main

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.

bash
git branch -m master main

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:

bash
git config --global init.defaultBranch main
git init

For one-off exceptions:

bash
git init -b release

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 main to 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.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions