loading...
Play Button Pause Button

Changing the default branch name on GitHub

nocnica profile image Nočnica Fee ・1 min read

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'

Discussion

pic
Editor guide
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
jcolag profile image
John Colagioia

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 Fee Author

Awesome! I look forward to reading it.

Collapse
memitaru profile image
Ami Scott (they/them)

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

Collapse
michaeltharrington profile image
Michael Tharrington (he/him)

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

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 Fee Author

Thank you! I need to update this post :)

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 Fee Author

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
hybriscole profile image
Alberto Cole❌

late capitalism problems™

Collapse
alfuken profile image
Info Comment marked as low quality/non-constructive by the community. View code of conduct
Bohdan Schepansky

It's horrible that the politics have came here, to IT. Who's next victim of SJW and "political correctness" going to be? Is it Blacklists? man pages? Servers? How did we even come to this?..

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.

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