Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change default branch via CLI

Tags:

github

I have been looking for a way to change the default branches of a bunch of my repositories (>50). Thus I am trying to make a bash script to do it for me.

However every answer I find, e.g. Deleting remote master branch, refused due to being the current branch, tells me to "go to the setting page on GitHub".

Can you do it without the UI?

like image 441
David Muñoz Tord Avatar asked Sep 17 '25 13:09

David Muñoz Tord


1 Answers

You can do it directly with the gh repo edit command of the GitHub CLI like this:

gh repo edit {owner}/{repo} --default-branch main

or if you have set a default remote repository, you can execute the command in the local repository without specifying the owner & repository:

gh repo edit --default-branch main

Please note that the new default branch must already exist, otherwise there will be a validation error.

like image 190
Matt Avatar answered Sep 19 '25 08:09

Matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!