Rename Git Branch

Menu

This is something I occasionally have to do and I usually get it wrong. So, for my reference, the next time I need it.

  1. Rename the old branch and push the new one.

    git rename -m master main
    git push origin main
    
  2. Change the default branch on the server. This can be done in the repository settings, if you are using Gitea or Github. If you use Gitolite, you need to log on the server and edit the HEAD file, changing the main reference to the new name:

    $ cd repo.git
    $ cat HEAD
    ref: refs/heads/main
    

    (Solution found on StackOverflow.)

  3. Push the deletion of the old branch.

    git push -u origin main
    git push -d main