Search code examples
gitgithub

How do I change the default branch for my GitHub repo, given that the settings entry does not exist?


To change the default branch, the instructions send us to Settings-> Code and Automation-> Branches. In my new repos, this option does not exist. In old repos, it does.

The option is missing both in repositories that are inside an organization, and outside an org, and repositories that are not in an organization, so that is not the cuase.

Why is this? How can I change the default branch name in these repos?

1. Here, the option exists

enter image description here

2. Here, the option does not exist

enter image description here


Solution

  • You have no commits in your repository on GitHub, so it makes no sense to speak of a default branch or any branch1. If you had added a README or other file to your repo when you created it, you would see the option shown in your first screen shot. But you decided to create a completely empty repo2. So you won't see that option until you push from a local. Then, that option will appear.

    Example! We create our repo at GitHub, calling it testing. As you show in your second screen shot, there is no default branch option. Now, on my own machine:

    % mkdir testinggit
    % cd testinggit
    % git init
    # make a file...
    % git add .
    % git commit -minit
    % git remote add origin git@github.com:myusername/testing.git
    % git push origin @
    

    Now go back to GitHub and refresh the page, and sure enough, there's your missing option to set the default branch. The default branch is, by default, the one you push first, which in my case was main. Which is exactly what I wanted.

    It might be useful also to realize that the concept of a "default branch" is pretty much bogus from a Git point of view. It isn't used for much of anything. I think it might be useful if you're going to do pull requests at GitHub, though, as it will be the branch chosen as the target branch for merging into by default when you create a pull request.


    1. Though, to be sure, Git does have a notion of an "unborn branch". But let's not get into that now.
    2. Perfectly reasonable.