DEV Community

Play Button Pause Button
Nočnica Mellifera
Nočnica Mellifera

Posted on

Changing the default branch name on GitHub

While the tech industry struggles to address injustice in our society, a small step you can take today is to remove the use of "Master/Slave" terminology from your tools.

git still calls the default branch of repositories 'master', a holdover from BitKeeper, which had both master and slave branches.

It's a bit surprising that this is a bit difficult to stop naming branches 'master'! While you can create a copy of 'master' with a new name, you can't delete 'master' without using the GitHub web interface.

From the cli you can move your branch to a copy (named 'main' in this example) with

git branch -m master main

then push it to GitHub with

git push origin main

But master will still be on GitHub, and you can't delete it since it's the default branch! You'll need to go to github.com and go to Settings>Branches to change the default. Only then can you delete the branch from github with

git push origin --delete master

So how do we make sure that all future repositories we create don't use 'master'?

Thankfully André Arko wrote a useful post on the subject

function git() {
  command git "$@"
  if [[ "$1" == "init" && "$@" != *"--help"* ]]; then
    git symbolic-ref HEAD refs/heads/main
  fi
}
Enter fullscreen mode Exit fullscreen mode

With this, every time we initialize a repository with git init we will immediate move it to use the branch name 'main'

Latest comments (13)

Collapse
 
modestfake profile image
Vitalii Saienko

With the latest git version 2.28 you can set the default branch name globally with this command:

git config --global init.defaultBranch main

Git v2.28 highlights

Collapse
 
nocnica profile image
Nočnica Mellifera

Thank you! I need to update this post :)

Collapse
 
hybriscole profile image
Alberto Cole❌

late capitalism problems™

Collapse
 
raynoldshelen profile image
Helen Raynolds

How do you add the git function to git itself so that it's run whenever we init a repo?

Collapse
 
nocnica profile image
Nočnica Mellifera

I do not think there’s a good way to do that, though anyone who clones a repository so initiated will have the same branch naming scheme.

Thankfully GitHub is looking at changing default naming soon :)

Collapse
 
michaeltharrington profile image
Michael Tharrington

I hope the workshop goes well today @nočnica! This is definitely an important topic.

Collapse
 
ben profile image
Ben Halpern

This is a highly disingenuous comment Bohdan.

Collapse
 
linaran profile image
Deni

Too aggressive on his part and the original issue is beyond politics, I agree.

But there is a point in there. Git certainly isn't the only thing using the master/slave terminology. There's a lot of refactoring (perhaps total rebuilding) to be done worldwide if we want to be consistent about this.

Btw, for transparency purposes stackoverflow clearly shows the number of downvotes on answers that were non-constructive. As far as I can tell the invisible hand of the "community" faded away that comment, although that comment has above average hearts.

Thread Thread
 
stephenrjohnson profile image
LimaBean Consulting

While it may be poorly worded, the fact that is has nearly double the amount of hearts would suggest that it is a shared sentiment.

Collapse
 
jcolag profile image
John Colagioia (he/him)

Very nice! I've been going through my repositories updating them (I'll probably post something about it on Wednesday or Thursday, myself), but something that hadn't occurred to me was deleting the original branch.

Collapse
 
nocnica profile image
Nočnica Mellifera

Awesome! I look forward to reading it.

Collapse
 
rozbarnes profile image
Roz Barnes

Thanks so much! I'm new to coding, but those terms have made me feel icky. Thank you, thank you!

Collapse
 
memitaru profile image
Ami Scott (they/them)

Thanks for this! I actually forgot you can change the default branch on github 😅😅

Some comments have been hidden by the post's author - find out more