GitLab rename master branch to main

The following steps show how to rename the master branch to main in GitLab.

  • Create the repository in GitLab as you normally would
  • On your laptop, clone the repository locally
    git clone git@git.dartmouth.edu:netid/myapp.git
  • If the repository is empty, add a README.md file because the next steps will not work on an empty repository
    cd myapp
    touch README.md
    git add -A
    git commit -m 'Initial commit'
    git push
  • Create main branch locally and then push it to the remote
    git branch -m master main
    git push -u origin main
  • In GitLab, remove protection from master and add it to main
    • Browse to https://git.dartmouth.edu/netid/my-app
    • Click "Settings", then "Repository"
    • Next to "Protected Branches", click "Expand"
    • In the list of branches, click "Unprotect" on the "master" branch
    • Click "OK" to confirm
    • On the "Protect a branch" section, choose:
      • Branch: main
      • Allowed to merge: Maintainers
      • Allowed to push: Maintainers
    • Click "Protect"
  • In GitLab, change the default branch to main
    • Still in the Repository Settings, next to "Default Branch", click "Expand"
    • Set "Default Branch" to "main"
    • Click "Save changes" 
  • Delete the master branch
    git push origin --delete master
  • If there are other clones of the repo (perhaps by team members), those can be updated with the following steps
    cd path/to/repos/myapp
    git checkout master
    git branch -m master main
    git fetch
    git branch --unset-upstream
    git branch -u origin/main
100% helpful - 1 review

Details

Article ID: 130751
Created
Sat 3/20/21 2:10 PM
Modified
Sat 3/20/21 2:13 PM