DEV Community

Prathamesh Sonpatki
Prathamesh Sonpatki

Posted on

3

Changing default git branch to main locally

git main branch

GitHub has shifted to main branch for new projects but when creating a new project locally with git init still creates master branch. In this post, we will see how to create main branch locally by default.

The git init command accepts value for name of the branch in the -b flag.

$ git init -b main
Initialized empty Git repository in /private/tmp/food/.git/
$ touch tmp.txt
$ git add .
$ git commit -m "main"
[main (root-commit) 0b3550b] main
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 tmp.txt
Enter fullscreen mode Exit fullscreen mode

This is possible only with Git 2.24 onwards.

We can also configure the default branch globally.

git config --global init.defaultBranch main
Enter fullscreen mode Exit fullscreen mode

For older Git versions, we can switch to the main branch before committing anything because the branch doesn't exist for real until something is committed.

cd sport
git init
git branch -m main
Enter fullscreen mode Exit fullscreen mode

My git version was 2.31.1 while writing this post.

git --version
git version 2.31.1
Enter fullscreen mode Exit fullscreen mode

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay