4

I would like to create a new git repository using git init command with branch name as prod. I searched all options supported by git init command in this documentation to override the default branch name master.

Unfortunately, there's no such option.

Customizing the source code of git is the only option?

  • What situation are you in that an empty repo looks like a good thing to have available for people to clone? – jthill Aug 12 '16 at 20:19

3 Answers3

4

With git 2.28.0, the default branch name can be configured with the setting init.defaultBranch

git config --global init.defaultBranch prod

More Info: https://github.blog/2020-07-27-highlights-from-git-2-28/#introducing-init-defaultbranch

TheKojuEffect
  • 20,103
  • 19
  • 89
  • 125
2

If it is a new git repository, you can just do

git checkout -b prod

And you will stand on a new branch named "prod".

Or, just rename the master branch if you already have committed some thing to it:

git branch -m master prod

This code can make a new repository with a branch named "prod"

mkdir repo
cd repo
git init && git checkout -b prod
吳仲昇
  • 29
  • 4
  • Getting this error, **error: refname refs/heads/master not found fatal: Branch rename failed** –  Aug 12 '16 at 18:43
  • If there is nothing in the master branch(You have not committed any thing to it), you cannot rename it because the branch doesn't exist. – 吳仲昇 Aug 12 '16 at 18:45
  • You can commit some thing and then rename it, or just checkout a new branch. – 吳仲昇 Aug 12 '16 at 18:46
  • Ok. I get your point. But I am looking for a solution that doesn't involve any commits. Just like how `git init` initializes an empty repository with branch **master**, I need a similar command for initializing an empty repository with branch **prod**. Am sorry, if my question was not clear. –  Aug 12 '16 at 18:53
  • Just try `git init && git checkout -b prod` I think this is what you want. This command will create a new repo and checkout a new branch "prod". Because master branch has nothing, it will disappear. – 吳仲昇 Aug 12 '16 at 18:55
  • Cloned worktree from this empty repo shows branch as master. –  Aug 12 '16 at 19:04
  • Yes, because your repo is empty, clone it is just like `git init`. Is your question how to clone empty repo and customizing the name of master branch? Which branch you first commit to will become the "master branch" of the repo. – 吳仲昇 Aug 12 '16 at 19:26
  • Just to clarify, my question is about creating an empty repository with branch name as `prod`. Users cloning this new empty repository should see branch as `prod`. –  Aug 12 '16 at 19:32
0

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.

enter image description here

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.

Community
  • 1
  • 1
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167