In order to change the default branch that users have checked out when they clone, you have to create a HEAD
pointer on the remote repository. The reference would look something like this in your local repository by running:
git branch -avv
remotes/origin/HEAD -> origin/master
By default, this is master
, but you can change the default branch in Github. Other hosting services have similar methods discussed in this question. If you are self-hosted or have shell/file-system access to the remote server, then you can manually set this with:
git symbolic-ref HEAD refs/heads/prod
After fetching the remote, you should see this updated:
remotes/origin/HEAD -> origin/prod
Now when users clone your repository, then they will automatically have the prod
branch checked out.
However, your specific question in regards to init
has led me to discover an interesting oddity with Github - even if the repository only has one branch named prod
it doesn't automatically create the HEAD
pointer.
Let's create a bare repository, set the branch name, and then commit:
git init
git checkout -b prod
touch file1
git add .
git commit -m 'init repo'
Create the remote repository:
curl --data '{"name":"myproject"}' -u YOUR_GITHUB_USERNAME https://api.github.com/user/repos
Add the remote and push:
git remote add origin https://github.com/YOUR_GITHUB_USERNAME/myproject.git
git push -u origin prod
To test this out, let's hop into a new folder and clone the project to see what it will look like for others.
git clone https://github.com/YOUR_GITHUB_USERNAME/myproject.git
Cloning into 'myproject'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
Checking connectivity... done.
warning: remote HEAD refers to nonexistent ref, unable to checkout.
Especially note that last line:
warning: remote HEAD refers to nonexistent ref, unable to checkout.
For some reason Github didn't create that HEAD
pointer for us, so git by default is going to fallback to using the master
branch, which doesn't exist in our repo. So when you cd
into the new folder you'll notice that you actually have checked out a bare branch called master
with no commits, yet the prod
branch is there if you just check it out. This is very annoying, so it seems like we will still have to manually update the default branch on Github.
This is where it gets weird. Github already shows that branch as the default, so we can't update it.

The only way I figured out how to get around this was to create another temporary branch, change it to the default branch, and then change it right back.
Now when anyone clones it, then they will have the prod
branch automatically checked out.