How to Create a Master Branch in a Bare Git Repository
You can only push and pull from an empty git repository. You are likely to encounter errors when you try to checkout references in a bare git repository.
This article will discuss creating a master branch in an empty git repository.
Create a Master Branch in a Bare Git Repository
As we had mentioned earlier, you cannot do much in an empty git repository. Let’s try to checkout the master branch in a bare git repo.
We will create an empty directory called Test-Repo.git
and initialize a bare repository.
Command:
$ git init --bare
Output:
We can not proceed with development in a bare git repository. To create a master branch, we must create a test-clone
repository and clone it there.
We will create a test-clone
folder and initialize a git repository.
Command:
$ git init
Output:
Next, we will clone our bare repository to our test-clone
repo.
Command:
$ git clone C:/Test-Repo.git
Output:
Then create a README.md
file and commit it to our test-clone
repository.
Command:
$ touch README.md
Next, we will add the file for commit.
Command:
$ git add README.md
$ git commit -m "Initial Commit"
Output:
What’s left is to push our master branch and the changes to the bare git repository.
Command:
$ git push C:/Test-Repo.git master
We have treated our bare repository as a hosted server, but instead of git push origin master
, we used the path to our bare repo.
The command above should create a master
branch in our bare repo with our "Initial Commit"
.
Output:
Let’s check if we have the master
branch and its commit in our bare repository.
Command:
$ git branch
$ git log
Output:
Conclusion
You cannot develop in a bare repository. Your best option is to clone it to another git repo and push a branch from there.
When cloning, be sure to enter the right path to the bare repo.
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedIn