0

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?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 1
    You can do it through the _API_ - per e.g. https://stackoverflow.com/q/52776313/3001761 - which you could certainly do from a bash script. Or do you mean the specific GitHub CLI, in which case did you [read the fine manual](https://cli.github.com/manual/gh_repo_edit)? – jonrsharpe Dec 30 '22 at 10:30

2 Answers2

2

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.

Matt
  • 12,848
  • 2
  • 31
  • 53
0

So deep down the rabbit call of the API doc I found that you can do such thing:

gh api repos/{owner}/{repo} --method PATCH --field 'default_branch=main' --silent