Navigating the master to main branch transition
Tags: git
Published: 2021-01-20 03:49:35

Many people are transitioning their branch naming conventions to switch from using the name `master` to using `main` ([statement from the git project](https://sfconservancy.org/news/2020/jun/23/gitbranchname/)). This post won't aim to show you how to rename your `master` branch as there are many [posts](https://dev.to/search?q=%22master%20to%20main%22&filters=class_name:Article) and [resources](https://github.com/github/renaming) focusing on that.  Instead this post will discuss a strategy for dealing with being *one-foot-in* and *one-foot-out* with several repositories (or even better not having to think about it at all).

If you work on small number of projects, this may not be an issue for you, however; as varying projects may or may not adopt the new naming conventions (and on varying timelines) it can be a bit cumbersome to constantly switch between using one vs. the other (i.e. muscle memory).

If this describes you, you may have seen this a lot recently:

![git checkout branch error](https://dev-to-uploads.s3.amazonaws.com/i/b6bewrnrfhccos16u3eb.png)

But fear not, **git aliases** (and a little bash) are here!

![for the win](https://dev-to-uploads.s3.amazonaws.com/i/envc4uj07c27zzw2o5tt.gif)

I ❤️ git aliases. Once I became comfortable with my git workflow, I was able to set aliases to create shorthands for some of the commands I would find myself using most often (e.g. `git cam` aliased to `git commit --amend`).

I noticed that the number of times that I actually typed `master` or `main` was relatively low, and I had already aliased a few (which had `master` hardcoded so annoyingly unusable in projects that used `main`). The instances that I was still typing the branch name were primarily when rebasing and checking out. Armed with this knowledge I set to work.

Before I added any aliases I need a way to tell if the current git repository has a `main` or `master` branch. There are a number of ways to accomplish this, and I'll likely swap out implementations down the line. I've started out by adding a simple script into my path that can return `main` if the current repository has a `main` branch, otherwise return `master`.  The command `show-ref` can be used to determine if that branch exists otherwise it will return nothing.

```shell
$ git show-ref refs/heads/main
SomeGitSha... refs/heads/main
```

A quick script that can use this command to then return if the current repository has either `master` or `main` could look something like:

```shell
if git show-ref refs/heads/main | grep -q 'refs/heads/main'
then
  echo 'main'
else
  echo 'master'
fi
```

I set this up to be executed when running the command `main_branch_name`, which I can now consume in my git aliases.

In order to interpolate the `main_branch_name` command into git aliases, I need to wrap the command in an anonymous bash function (e.g. `!f() { command }; f`). I noted all the times I was typing out `main` or `master` and added the following aliases to my gitconfig, which so far has covered all my needs.

```gitconfig
com = "!f(){ git checkout $(main_branch_name); };f"
rbm = "!f(){ git rebase $(main_branch_name); };f"
rim = "!f(){ git rebase -i $(main_branch_name); };f"
```
As I come across new instances of needing to physically type out the name of the main branch, I add an alias and start training myself to use that instead.

Hopefully this post can help you navigate this transition in a way that works for yourself!