Configure git to use 'main' as the primary branch

Overview

When you initialize a new git repository via SSH on a DreamHost server, the primary branch is named master. This is because the git software currently defaults to this branch name.

Many companies that use the git software as the basis of their platforms, such as github.com, gitlab.com, and bitbucket.org, have renamed this primary branch to main. The following links give you more information about this topic:

What does this mean?

When you create a repository on your DreamHost server, the primary branch is named master. This will continue to function normally on your DreamHost server without any issue.

However, it's recommended that going forward, you rename the primary branch name to main for your repositories. This will ensure future compatibility with other software programs, as mentioned above.

Changing the primary branch to use 'main'

Newly created repositories use the name master as the primary branch. To ensure future compatibility, it's recommended you now update this branch name to main.

  1. Check the name of your branch by running the following command on your home computer or DreamHost server.
    [server]$ git branch -a
    If there are multiple branches, the active branch will display a * to the left. If you see the primary branch is master, run the following commands to update it to main.
  2. Make sure the active branch is master.
    [server]$ git checkout master
  3. Rename it using the -m option.
    [server]$ git branch -m main

    If the main branch exists for some reason, you can use -M to force the rename.

  4. Update symbolic refs to use main.
    [server]$ git symbolic-ref HEAD refs/heads/main

Updating remote repositories

If you have pushed this local repository to a remote repository (such as github.com), you must push it again to reset the upstream branch.

Change REMOTENAME to the name of your remote repository. Run git remote to view existing remotes.

[server]$ git push -u REMOTENAME main

You should then delete the old master branch.

[server]$ git push REMOTEBRANCH --delete master

View the following link for further details.

Updating your git config file (on your home computer)

The following command only works with git versions 2.28 or higher. DreamHost servers run the git version installed with the server's Operating System, which is currently below version 2.28. To confirm, log into your server via SSH and run the following command:

[server]$ git --version

If the version is below 2.28, you must use the option above to manually rename the branch.

If the version of git on your home computer is above 2.28, you can run the following command to add a line to your git config file. This will ensure all future repositories you create (on your home computer) use main as the primary branch. You can then push them to your DreamHost server using the instructions in the following articles:

Running the command

  1. Open a terminal on your home computer.
  2. Run the following command:
    [server]$ git config --global init.defaultBranch main

Now when you initialize a new repository by running git init, main will be used instead of master as the primary branch.

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?