Skip to main content

Changing the default branch in Git from master to main has become a common practice. 

If you tried this

git push origin --delete master 

and got this error:


remote: error: By default, deleting the current branch is denied, because the next 
remote: 'git clone' won't result in any file checked out, causing confusion. 
remote:  
remote: You can set 'receive.denyDeleteCurrent' configuration variable to 
remote: 'warn' or 'ignore' in the remote repository to allow deleting the 
remote: current branch, with or without a warning message. 
remote:  
remote: To squelch this message, you can set it to 'refuse'. 
remote: error: refusing to delete the current branch: refs/heads/master 
To [REDACTED]

Read on .... 

The steps to change the git default branch from master

Step 0: Make a backup. 

Make a backup of the directory. Always easier to just delete a directory and start over if you mess up any of the steps below on the local repository before you've pushed to the remote repository. 

Step 1: Have a new local branch that's not master

If your current master is up to date and all you want to to is rename it, then you can rename the local master branch to main as.... 

git branch -m master main
  • -m: This option stands for "move" or "rename".
  • master: The current name of the branch.
  • main: The new name for the branch.

If, however, you have a new branch that you want to use and master is an older and out of date branch, then delete the local master branch. (did you make a backup in step 0?)

git branch -d master

Step 2: Push the replacement local Branch to the Remote Repository 

Assuming you are using "main" as your replacement branch. Push the main branch to your remote repository and set it as the default tracking branch.

git checkout main
git push -u origin main
  • push: This command uploads your local changes to the remote repository.
  • -u: This option sets the upstream for the branch, which means it will set the remote main branch as the default tracking branch for your local main branch.
  • origin: The default name for the remote repository.
  • main: The new name for the branch.

Step 3: Set HEAD in your local repository to point to main.

git remote set-head origin main

To check this run 

git branch -v -r

and you should see something like

 origin/HEAD                      -> origin/main 
 origin/devel                     a1a1a1a1 Commit description 
 origin/main                      a0a0a0a0 Other commit description

Step 3: Set HEAD on your remote repository to point to main (i.e. Default Branch in the Remote Repository)

This step depends on the hosting service you are using (e.g., GitHub, GitLab, Bitbucket, gitolite).

Gitolite

  • ssh to your gitolite server
  • change into an account that has write access to those repositories. The default account for .deb packages is "git" e.g.
    sudo su - git 
     
  • find the directory that holds your repo.  If you logged in as the gitolite user then it would be 

    "~/repositories/REPONAME.git" which is likely a full path to " /var/lib/gitolite3/repositories/REPONAME.git"
    where REPONAME is the name of your project

  • edit the file HEAD (e.g. vi /var/lib/gitolite3/repositories/REPONAME.git) and change 
    ref: refs/heads/master
    to
    ref: refs/heads/main
     
  • Save the file

GitHub

  • Go to your repository on GitHub.
  • Click on the "Settings" tab.
  • In the left sidebar, click on "Branches".
  • In the "Default branch" section, click on the pencil icon to edit.
  • Select main from the dropdown menu and click "Update".
  • Confirm the update by reading the instructions and clicking the appropriate buttons.

GitLab

  • Go to your repository on GitLab.
  • Click on the "Settings" menu on the left sidebar and select "Repository".
  • In the "Default branch" section, click the "Expand" button.
  • Select main from the dropdown menu.
  • Click "Save changes".

Bitbucket

  • Go to your repository on Bitbucket.
  • Click on the "Settings" tab.
  • In the left sidebar, click on "Repository details".
  • In the "Main branch" section, select main from the dropdown menu.
  • Click "Save".

Step 4: Delete the Old master Branch on the remote  server

After you have updated the default branch in your remote repository, you can now delete the old master branch.  

git push origin --delete master
  • push: This command uploads your local changes to the remote repository.
  • origin: The name for the remote repository.
  • --delete: This option tells Git to delete the branch.
  • master: The name of the branch to be deleted.

Step 5: Update Local Repositories

If other developers are working on the same repository, they will need to update their local copies to reflect the changes. These commands will update the local repository to track the new main branch and set it as the default branch.

git fetch origin
git branch -u origin/main main
git remote set-head origin -a
  • fetch origin: This command downloads objects and refs from another repository.
  • branch -u origin/main main: This command sets the upstream branch for the local main branch.
  • remote set-head origin -a: This command updates the remote-tracking branch.
Tags