Changing Git’s default branch

In the wake of the protests surrounding George Floyd’s death and the resurgence of the #BlackLivesMatter movement, there has been a lot of talk in the tech community about changing some of the terms we use. There was a lot of focus on moving away from using master and slave for describing communications protocols. There was also discussion about changing blacklist and whitelist. Apparently even NIST is getting on board with this. The racial overtones of these terms are pretty obvious so changing them seems to make a lot of sense.

There is another place where we use the term master and that is with Git. The default branch name in Git is master. Now there is some debate about whether it refers to slavery and whether people should be offended by it or not. It doesn’t really matter. Apparently at least some people are and it’s easy enough to change, so I say why not.

Now let’s not pat ourselves on the back. It is certainly a small gesture. Changing one arbitrary word isn’t going to do anything to solve the problem of systemic racism in the United States. However, it signals that we in the tech community are at least willing to acknowledge that racism is an issue and it might make some minorities feel more comfortable working in tech.

Now if you don’t immediately go out and change all your git repositories, I’m not mad at you. It doesn’t make you a bad person or a racist. I get it that it takes some effort and could cause some problems. In fact, I wrote a series of blogposts on git before all this happened. They just recently got published. They still use the name master. I haven’t gone back and changed them yet. I might, but it is pretty low on my priority list. However going forward, it is very easy to change for future repositories, so again why not?

For new projects

# create alias
git config --global alias.new '!git init && git symbolic-ref HEAD refs/heads/main'
# now git new will create a new repository with main as the main branch. Use this instead of git init.
git new

For new projects, simply use the git config command above to create an alias. Then use git new instead of git init to create any new repositories and you will have a repository where the default branch is main. Main is a nice choice if you use the CLI since master and main both begin with “ma” so tab completion will work the same as before. Looking at the code above, it is trivial to set the default to something else. In the git config command, just replace main with whatever you would like. Production might be a good choice.

Here is what it looks like in Git Bash. Note that after git new we are now on the branch named main.

For existing projects

For existing projects, here are some commands that you can run on your local clone. Note it requires logging into Github or GitLab and updating the setttings there. If there is no remote, then you can stop after the first 2 commands.

# make sure you are on master
git switch master
# rename master to main
git branch -m master main
# push your new main branch upstream
git push -u origin main
# At this point go to your GitLab account
# If the default was master, you'll need to change the default to point to main
# You'll also need to set master to unprotected so you can delete it
# Then re-add that protection to main
# then return to the terminal
# delete your tracking branch and push upstream
git push origin --delete master
# then update the remote HEAD
git remote set-head origin -a

Updating Clones

The above commands work well, but they won’t update all the clones that already existed. For that use this script below. Run it in each clone.

# make sure you are on master
git switch master 
# rename master to main
git branch -m master main
# fetch the latest commits
git fetch
# remove link to origin/master
git branch --unset-upstream
# instead add link to origin/main
git branch -u origin/main
# update the remote HEAD
git remote set-head origin -a
# now remove your old tracking branch
git remote prune origin

NOTE: if you haven’t made any local changes, it may be quicker/easier to just delete the local copy and clone it again.

Side Effects/Unintended Consequences

If you are going to make this change, you will want to go through your CI build scripts and replace master with main. If you have other scripts that you run locally to automate some tasks around repository management, you probably want to check those as well. If you are using Git Flow, you should update the entries in your .git/gitconfig file.

Real change

If you are interested in promoting real change and racial equality, it will take more than simply updating a few git repositories. Protesting is great, but there are plenty of other ways to help the cause besides protesting. If you are in charge of hiring, go specifically seek out and hire some black engineers. If you already have black engineers working for you, work to get their contributions recognized and get them promoted. When black people post about racism on social media, share and amplify their voices. When you see racist behavior don’t be afraid to call it out. Whatever you do, find some way to take real meaningful action.

Here are a couple petitions worth signing. Do a little research about these cases. I think you’ll see that in both these cases the police have not been held responsible for their actions. That needs to change. It won’t fix everything, but it is a good start.

References

I didn’t come up with all these commands myself. They were inspired by these 2 sites:

2 Comments on “Changing Git’s default branch

  1. Nice move.
    I have the same thought as you. It is definitely not a word that it is going to change everything, but it is a signal for it.
    Regards,

    • Glad you agree. It is a small thing, but if it helps at all, then it is worth the very small amount of effort.

Leave a Reply

Your email address will not be published. Required fields are marked *

*