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. Finally, 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.

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?