Charlène's

... it's worse than your datacenter.


How i've moved my git repos branches from master to main (non mainstream version)

Latest update: 2021-01-02

I won't discuss the whole affair, as it has been done elsewhere many times. Tutorials are everywhere to do so, yet, i had two problems:

I am quickly explaining the whole process here. For reference; WWWSRC is the root directory of all your public git repositories.

Warn your users; set a deadline

In my case, and according to the httpd logs, it was acceptable to do without that, but in a more popular one you want to give them a deadline.

I'll detail below how to update their cloned repo.

Backup your stuff

I strongly advise to backup your remote git repositories. I have myself lost all my remote repos while experimenting, but since i do daily backups i was hopefully able to recover quickly ;-)

Modify your git frontend (mostly apply to gigit, your mileage may vary)

The first step was to change my git frontend, and modify all the references from the master branch to the main one:

Once everything was ironed out, i had now to deal with the repositories.

From here, i should have done this out of production. In that case, you may want to copy WWWSRC to WWWSRCNEW and clone all the repos with the temporary remote for conversion. Once the conversion is done it is just about replacing WWWSRC by WWWSRCNEW.

Convert your repos (in 2 easy but tedious steps)

I proceeded like this for each of my repositories.

On my remote bare git repos, i had to do this to make 'main' the default branch:

 $ cd $WWWSRC/myrepo.git
 $ vi hooks/hooks/post-receive
 $ #  ^ and rename all master 'branch' reference to 'main'. sed could do the job.
 $ git symbolic-ref HEAD refs/heads/main

The problem right now is that the remote repo has no content for the main branch, that does not exist yet. Time to feed it:

On my local repos then, i moved the branch like this:

 $ git branch -m master main
 $ git checkout main
 $ git push -u origin main
 $ git push origin --delete master
 $ # and now do an empty yet informative commit to see the result:
 $ git commit --allow-empty  -m "master branch renamed to main"

Change your git config to define the initial branch name

This requires git>=2.28:

 $ git config --global init.defaultBranch main

Update your cloned repos (for you and your users)

Your users (and you if you did the conversion out of production) will either need to clone again repos, or update them.

While cloning again is indeed easier, here is how to convert a cloned repo:

 $ git checkout master
 $ git branch -m master main
 $ git fetch
 $ git branch --unset-upstream
 $ git branch -u origin/main