2022.08.10 Rename Git branch from master to main - GlenKPeterson/One-off_Examples GitHub Wiki
The only piece of this that shouldn't have been obvious was the idea that the main bare repo on the server had the concept of a checked-out branch and would not delete that branch. I learned that here: https://stackoverflow.com/a/9263023/1128668
Otherwise, this is a fairly simple process.
Steps
Before starting, make sure anyone who might pull from this repo is informed!
DESKTOP$ git --version
git version 2.17.1
Move the branch locally
DESKTOP$ git branch -m master main
DESKTOP$ git status
On branch main
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
Push the moved branch to origin
DESKTOP$ git push -u origin main
Total 0 (delta 0), reused 0 (delta 0)
To ssh://server:/opt/git/bareMainRepo.git
* [new branch] main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
Delete the old branch
DESKTOP$ git push origin --delete master
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 ssh://server:/opt/git/bareMainRepo.git
! [remote rejected] master (deletion of the current branch prohibited)
error: failed to push some refs to 'ssh://myname@server:/opt/git/bareMainRepo.git'
Oops. This is where you need to go to the upstream origin server and change which branch is "checked out" in the (presuably) bare repo there
DESKTOP$ ssh server
SERVER$ cd /opt/git/bareMainRepo.git/
SERVER$ git --version
git version 2.17.1
SERVER$ git branch
main
* master
SERVER$ git checkout main
fatal: this operation must be run in a work tree
Oops. Bare repos don't expect this command. Give it the current directory as a work tree to make it happy
SERVER$ git --work-tree=. checkout main
Switched to branch 'main'
SERVER$ git branch
* main
master
SERVER$ exit
Done switching the "checked out" branch of the upstream repo. Now go back to the local repo and push a normal branch delete
DESKTOP$ git push origin --delete master
To /opt/git/bareMainRepo.git
- [deleted] master
Make sure anyone who might pull from this repo is informed!