Change Git's Init Branch Name Default |Video upload date:  · Duration: PT2M33S  · Language: EN

Change the default branch name created by git init globally or per repo using git config or the git init -b option Learn commands and verification tips

If you are tired of renaming the first branch after every git init like some kind of medieval manual laborer welcome to the future. You can tell Git which branch name to create by default and save yourself a few awkward commits and a lot of tiny curses. This short guide covers setting the default branch globally or per repository and how to override it when you create a repo from scratch.

Why change the default branch

Most hosting providers and teams prefer a consistent default branch name. Left alone Git will keep doing whatever it always did which means manual work later. Picking and setting a default branch early avoids CI confusion automated scripts breaking and the awkward moment when someone opens a PR to the wrong branch.

Set a global default with git config

Want every new repo on your machine to start with main or any name you choose? Set the global config and be done with it.

git config --global init.defaultBranch main

This writes your preference to your global git config so git init will create a branch named main by default. Use this when you control your machine and want consistency across projects.

Set the default just for one repository

If you only need a specific repo to behave differently run the command inside that repo or point at its path from anywhere.

git -C path/to/repo config init.defaultBranch main
# or run this inside the repo
cd path/to/repo
git config init.defaultBranch main

This updates the local config and leaves your global setting untouched which is perfect for oddball projects or experiments.

Create a repo with a chosen branch right away

If your Git version supports it you can avoid config changes altogether when initializing a new repository. Use the -b flag with git init to pick the branch name at creation time.

git init -b main my-new-repo
cd my-new-repo
git branch --show-current

Note that the -b option was added in newer Git releases (around Git 2.28). If you get an error update Git or use the config approach above.

Verify your settings

  • Check what git init will use now by inspecting the config value
git config --get init.defaultBranch
  • Or test it by creating a temporary repo and checking the current branch
git init temp-repo && cd temp-repo && git branch --show-current

Common pitfalls and advice

  • Make sure your hosting service default branch and your CI pipelines match your chosen name to avoid surprises during automation
  • Set the global option early for new machines to prevent a mismatch across developers
  • Changing the default does not rename branches in existing repositories

Tip Pick a default branch name pick it once and tell your team. Then enjoy the tiny victory of never renaming the initial branch again.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.