How to Create a Master Branch in a Bare Git Repository

John Wachira Feb 02, 2024
  1. Create a Master Branch in a Bare Git Repository
  2. Conclusion
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:

Create an empty directory and initialize a bare repository

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:

Create a test-clone folder and initialize a git repository

Next, we will clone our bare repository to our test-clone repo.

Command:

$ git clone C:/Test-Repo.git

Output:

Clone the bare repository to the newly created repository

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:

create a file and commit it in the repository and add the file for commit

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:

Create a master branch in the bare repository with the initial commit

Let’s check if we have the master branch and its commit in our bare repository.

Command:

$ git branch
$ git log

Output:

Check if the master branch and its commit are in the bare repository

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.

Author: John Wachira
John Wachira avatar John Wachira avatar

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

Related Article - Git Branch