Home > database >  Change default branch via CLI
Change default branch via CLI

Time:12-31

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?

CodePudding user response:

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

CodePudding user response:

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.

  • Related