Changing git default branch from master to main on command line (cli)

I'd like to change my default branch from master to main - I have done the following locally:

git branch -m master main
git push origin main
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main

Now when I do:

git branch -a

I get the following:

* main remotes/origin/HEAD -> origin/main remotes/origin/main remotes/origin/master

When I try and delete the master branch I get an error:

! [remote rejected] master (deletion of the current branch prohibited)

On my git server when I do:

git branch

I get the following:

 main
* master

Contents on the remote (ll -a) are below:

drwxrwxr-x 7 ubuntu ubuntu 4096 Oct 21 08:56 ./
drwxr-xr-x 46 ubuntu ubuntu 4096 Sep 4 12:20 ../
-rw-rw-r-- 1 ubuntu ubuntu 23 Feb 23 2021 HEAD
drwxrwxr-x 2 ubuntu ubuntu 4096 Feb 23 2021 branches/
-rw-rw-r-- 1 ubuntu ubuntu 66 Feb 23 2021 config
-rw-rw-r-- 1 ubuntu ubuntu 73 Feb 23 2021 description
drwxrwxr-x 2 ubuntu ubuntu 4096 Feb 23 2021 hooks/
drwxrwxr-x 2 ubuntu ubuntu 4096 Feb 23 2021 info/
drwxrwxr-x 46 ubuntu ubuntu 4096 Oct 21 09:48 objects/
drwxrwxr-x 4 ubuntu ubuntu 4096 Feb 23 2021 refs/

So my question (finally) is how do I repoint my remote branch to main so I can delete master, locally and remotely?

2

1 Answer

You have to change the HEAD pointer directly on the server as well, not only on the clones using it as a remote. This can probably be done in two ways:

  • Change the remote HEAD to the new branch, then delete the old one:

    git symbolic-ref HEAD refs/heads/main
    git branch -d master
  • Or delete the new branch, then rename the old one (and re-push commits):

    git branch -d main
    git branch -m main

(Note that git remote set-head -a can be used on clones to automatically update origin/HEAD after it's changed on the server.)

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like