Default Git Branch Name with Learn Enough and the Rails Tutorial

Oct 24, 2020 • posted by Michael Hartl
This is a short post explaining the current state of the default Git branch name in the Learn Enough tutorials and the Ruby on Rails Tutorial. If you reached this post from a tutorial ebook that uses the master branch, you should download a free updated version, which has been changed to use main. For more a detailed discussion of version control with Git, see Learn Enough Git to Be Dangerous.

The Git version control system is designed to help you track changes in projects. As part of this, Git allows you to create separate “branches” for particular sets of project changes, which can then be incorporated (or merged) back into the default branch.

Upon Git’s initial release in 2005, the default Git branch was called master. Some developers, concerned about possible negative associations with this term, have advocated for using a different name for the default branch, such as trunk, default, or main.1 Most noably, in October 2020 developer platform GitHub announced that new repositories will use main by default. In practice, this means that GitHub’s instructions for new repositories use main instead of master, including a command that changes the default branch name on the local repository (Figure 1).

images/figures/github_instructions_main
Figure 1: The instructions for a default repository at GitHub.

As of this writing, Git itself still uses master by default, but there are plans to change to main in Git as well. Indeed, at this point most major Git organizations have changed or are planning to change to main as the preferred default branch name. As part of this transition, more recent versions of Git have added an additional configuration parameter called defaultBranch to specify the default branch name for new repositories.

Accordingly, the Learn Enough tutorial books (including the Ruby on Rails Tutorial) have been updated to use main. In addition, while it is impractical to update all 55+ hours of Learn Enough screencast videos, a note has been added to the Learn Enough Git to Be Dangerous videos indicating that users should make the substitution mastermain in the screencasts.

Because Git used master as the default branch name for the first 15+ years of its existence, there are a large number of existing Git repositories using that name for the primary branch, and there is a correspondingly large amount of documentation and third-party software that assumes this default branch name as well. As a result, making this mastermain substitution will be necessary for a long time to come, but luckily it’s not too difficult. Moreover, unexpected changes happen all the time in software development, so this is an excellent chance to apply one of Learn Enough’s core principles, technical sophistication. In the rest of this post, we’ll discuss a solution resulting from an application of this principle.

1 Updated Git version

In order to apply the solution in this post, it’s important to use a version of Git that supports the defaultBranch configuration parameter, which is included as of Git version 2.28.0. You can check your Git version at the command line as follows:

$ git --version
git version 2.31.1    # should be at least 2.28.0

If the version number isn’t at least 2.28.0, you should update your installation of Git as described in Section 1.1: Installation and setup of Learn Enough Git to Be Dangerous. In particular, cloud IDE users can run the command in Listing 12 and macOS Homebrew users can run the command in Listing 2.

Listing 1: Upgrading Git on the cloud IDE.
$ source <(curl -sL https://cdn.learnenough.com/upgrade_git)
Listing 2: Upgrading Git on macOS.
$ brew upgrade git

2 Going with main

Once the Git version has been verified (and, if necessary, updated) as in Section 1, we are ready to ensure that the default branch name is main. The first step is to configure your local system by setting the global configuration parameter defaultBranch to main, as shown in Listing 3. This command needs to be run only once on each computer you use for software development.

Listing 3: Configuring the default branch name.
$ git config --global init.defaultBranch main

The second step is to confirm that your default branch at GitHub is set to main. You can do this as follows:

  1. Sign in to your GitHub account.
  2. Go to github.com/settings/repositories.
  3. Confirm that the default name appears as main (Figure 2).
  4. If it is not main, change it to main in the text field and then click “Update”. The result should be as shown in Figure 2.
images/figures/default_branch_main
Figure 2: Confirming main as the default branch name at GitHub.

Note that the capabilities of Git and GitHub described above make it possible to use any branch name as the default. To use, e.g., trunk, dev, or default (or to revert to master), simply replace main with your preferred default branch name in Listing 3 and Figure 2.

1. Although many of the discussions on this subject have been influenced by controversy surrounding the use of master/slave terminology in technology, in fact the use of master in Git refers instead to the notion of a master recording. Lingering concerns remain, however, leading many developers to favor a change from master to main in any case.
2. The command in Listing 1 should actually work on any Linux system that supports the APT (apt-get) package manager.
MORE ARTICLES LIKE THIS:
learnenough-news , git , github