What is a Bare Git Repository?

While using git you must have come across some questions like:

  • If git is a Distributed Version Control System then where is the remote repo of git stored in our system?
  • Why can’t we directly push into or clone from our local repository?
  • Can I host my own private remote repository just like GitLab or GitHub for my own small project?
  • What if there was no GitLab or GitHub or Bitbucket or… I mean… you got the point, right?

Well… and answer to all these questions is the same and that is a Bare Git Repository.

LET’S GET STARTED

First things first, what is a Bare Git Repository and how is it different from a normal Git Repository.

Initializing A Local Git Repo | git init

A normal git repository is a repository where git is simply initialized using git init command. A git repository contains a hidden .git directory where all the important files for tracking the changes in the folders are stored. In simple words git init initializes a local repository for your files. It stores the hashes of commits made in the branches and a file where the hash of the latest commit is stored.

You can add, commit, stash your files, and add-on to that you can reset or revert your changes in your local repo. But you cannot push into or pull from this repository. For that, you need a remote repository.

Initialize a non-bare repo using git init command

Initializing A Remote Git Repo | git init –bare

Now talking about a Bare Repository, it is simply initialized by using git init --bare command. No commits can be made in a bare repository. Any changes made in the original projects cannot be tracked by a bare repository as it doesn’t have a working tree, ie. a directory where all the project files reside. Using --bare flag with git init simply initializes a private remote git repository which you can access using SSH over default port 22.

Generally, a git bare repository contains an extension of .git. In this repository you cannot commit any changes hence it cannot keep a track of the changes made in your files. But you can definitely push, pull, fetch or clone from it. In other words, you can use any git commands applicable to a remote repository.

Initialize a bare repo using git init –bare command

While looking at the list of files and directories from both bare and non-bare repos you can easily figure out that content of .git folder is exactly the same as remote.git repository.

Now the natural question that arises is, Can we actually and practically use it as our remote repository for our files? And if yes, then how?

How To Practically Use A Bare Git Repository?

The answer to the above question is DEFINITELY YES. Yes, we can use a bare git repo as a remote repo for our code. Let’s see how:

Step 1: Initialize A Bare Git Repository

You can easily initialize a bare git repository as discussed earlier, by using git init --bare command. The repository should preferably have a .git suffix.

Initialize a bare repo using git init –bare command

Step 2: Clone The Remote Git Repository

You can simply clone your remote repo using ssh protocol and the syntax will be in the order git clone ssh://username@publicIP/<absolute path of repo>

Cloned repo will not have a .git extension rather it’ll contain a .git dir.

Step 3: Create a File and Commit Some Changes

Simply add a few files and put some content into it, similarly committing 2-3 changes will help you visualize better.

Create a file and commit your first change
Again add another file and commit few other changes
Use git log to see the commit messages and id

Step 4: Push The Changes To The Remote Bare Repository

Now finally its time to push the changes into our remote repo using git push origin command.

Push your changes into your remote repo

Step 5: Changes Updated in Remote Bare Repository

By using git log command you can see similar changes in your remote repo. One thing to note here is that even after pushing your newly created files you cannot see them into your remote repo and thus cannot edit or make any changes to them.

Use git log command to see the commit messages

Step 6: Delete The Local Repo & Clone Again

Now in order to make sure that your remote repo is working fine and verify that all the commit messages are perfectly saved you can delete your local repo and clone again using the same URL.

Remove local repo using rm -rf and clone again
Verify the content of your files using cat command

Pros & Cons of a Bare Git Repo:

Pros:

  • One can get real practical exposure to using a distributed VCS.
  • You can have your own remote git repo independent of any third-party client eg: Gitlab, Github.
  • No need to create an account with a third-party client.
  • Practically good for a small team working remotely.

Cons:

  • Additional headache to maintain a remote server containing code.
  • Loss of server might result in loss of all the files.
  • Cannot visualize the files stored in our remote repo.

Conclusion

So far we have clearly learned the meaning of bare git repository and its dimensions. Along with that, we have also found out the difference between a bare repo and a non-bare repo. Furthermore, we have even explored how we can create our own remote repo and use it locally on remote servers.


Blog Pundit: Bhupender Rawat and Sandeep Rawat

Opstree is an End to End DevOps solution provider

Connect Us

3 thoughts on “What is a Bare Git Repository?”

Leave a Reply