Replacing master in git

damcosset

Damien Cosset

Posted on June 13, 2020

Replacing master in git

Master/slave denomination

The master/slave denomination is a common one in technology. Master/slave is an oppressive metaphor referring to the practice of slavery. These metaphors are inappropriate when describing concepts in technology. They are also inaccurate. Using these metaphors takes away the history of slavery and put it into a context where it does not belong.

UPDATE: People seem to push back on this, for whatever reason... Git's master is historically tied to master/slave. It got the name from BitKeeper. Source here

UPDATE 2: Github is looking to replace the master word

UPDATE 3: This blew up. I'll have to reflect on many things about this subject. I still think that the word should go away from git. Period. However, I was not the right person to brought that kind of topic on the table. I have done harm in the process and I apologize for that. I'm leaving this article up for accountability. Learning to shut up listening and prioritize Black voices is something I will work on.

Words matter. The words we use to define concepts have a lot of importance, even in our tech environment. Git is one of those environments where the word master is still used. But, it's not that complicated to take it away. We have to do two things:

  • Replacing the word on existing branches, both locally and remotely
  • Modify your git configuration to not use the word master when running git init

Let's start with the first point.

Replacing master from your existing projects

First off, we have to change our master branch locally.

Changing master to principal

I have here a project with a master branch. I'm running git branch -m master principal to rename my master branch into the principal one. This command keeps the history of the branch, so you won't lose anything!

Note: I chose to rename the branch as principal. You can choose another name if you wish. It has to make sense for you and your team.

Running git push -u origin principal updates the remote repository by adding the principal branch. The -u flag also sets the upstream.

Change the default branch on Github

Now, I also need to change the default branch on Github. In your repository page, click on the Settings tab, then branches on the left menu. You can update the default branch here:

Updating the default branch on Github

And you are done! If you run git log inside the principal branch, you will see that the history is intact!

Git log on principal branch

We started on origin/master and it properly show that we are on principal now!

Updating local clones

What if someone has a local clone of this repository, how would they correctly update their clone?

This tweet explains you how:


$ git checkout master
$ git branch -m master principal
$ git fetch
$ git branch --unset-upstream
$ git branch -u origin/principal
$ git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/principal

Note that the tweet uses the word main to replace master. I'm using principal. Just replace it in the commands with the name you chose.

What these commands do:

  • Go to the master branch
  • Rename the branch to principal
  • Get the latest changes from the server
  • --unset-upstream removes the link to origin/master
  • -u origin/principal creates the link to principal
  • Updates the default branch

Git init

When you run git init, the default branch is master. There are two ways to change that:

Using an alias

You can set up an alias that would run git init while having a other default branch name:

git config --global alias.new '!git init && git symbolic-ref HEAD refs/heads/principal'

Running the above command would allow you to use git new and it would have the principal branch as its default.

You can modify this command of course. Notice the alias.new, this can be change to alias.initialization for example, or whatever command you would like to make. Of course, you can also modify the principal name to fit your needs.

Modifying your git config

We can configure our git to change the default branch.

  • Find the configuration file of your git. It should be either in ~/.config/git/config or ~/.gitconfig.

  • Inside this file, add the following lines:

[init]
    templateDir = ~/.config/git/template/

Now, inside ~/.config/git/template ( you may have to first run mkdir ~/.config/git/template to create it), create a HEAD file and add this line, plus a line break.

ref: refs/heads/principal

When you run git init, the whole contents of the templateDir is copied into .git.

You can now run git init and you will have the branch principal as its default!

Of course, you can change principal to another name if you wish.

Alternative names

I've chosen to use the word principal to replace master here. Here are other words you can use:

  • main
  • primary
  • leader
  • active
  • parent

Just don't use master...

Have fun ❤️
Do no harm.

Sources

💖 💪 🙅 🚩
damcosset
Damien Cosset

Posted on June 13, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related