The Wayback Machine - https://web.archive.org/web/20240930231506/https://www.geeksforgeeks.org/bare-repositories-in-git/
Open In App

Bare Repositories in Git

Last Updated : 29 Dec, 2019
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Repositories in Git are a snapshot of the folder in which you are working on your project. You can track the progress and changes made to the project by making commits and also revert changes if not satisfactory.
Repositories can be divided into two types based on the usage on a server. These are:

  • Non-bare Repositories
  • Bare Repositories

What is a Non-bare repository?
A non-bare or default git repository has a .git folder, which is the backbone of the repository where all the important files for tracking the changes in the folders are stored. It stores the hashes of commits made in the branches and a file where the hash of the latest commit is stored.
The file structure of the default repository should look something like this:


    -- Default_Repo* 
|-- .git*
| |-- hooks*
| |-- info*
| |-- logs*
| |-- objects*
| |-- refs*
| |-- COMMIT_EDITMSG
| |-- config
| |-- description
| |-- HEAD
| |-- index
|-- example.txt
*: Folders

As you can see, the .git folder contains all the required files for tracking the project folder. The default repository is always used for local repositories.
 
What is a bare repository?
A bare repository is the same as default, but no commits can be made in a bare repository. The changes made in projects cannot be tracked by a bare repository as it doesn’t have a working tree. A working tree is a directory in which all the project files/sub-directories reside. Bare repository is essentially a .git folder with a specific folder where all the project files reside.
Practically speaking everything in the repository apart from .git is a part of working tree. To create a bare repository, navigate to the chosen directory in bash (for linux users) or command prompt (for windows users) and type:

 
>mkdir FileName.git 
>cd FileName.git
>git init –bare

Creating Bare Repository

The file structure of the bare repository should look like this:

-- BareRepo.git* 
|-- hooks*
|-- info*
|-- logs*
|-- objects*
|-- refs*
|-- COMMIT_EDITMSG
|-- config
|-- description
|-- HEAD
|-- index
*: Folders

Note: This is the exact same file structure of .git folder in non-bare repository

It is important to note that all bare repositories have .git extension (E.g. notice BareRepo.git). Since you cannot commit, or make changes to it, bare repositories are pretty useless on their own. But then why does it exist? When people collaborate to work on a project, they need a central repository where all the tracked changes are stored and prevent any conflict between the versions of the project on other’s computers. A central repository also means that any new contributor can clone the repository into a local one without getting any unsaved changes or conflicting work of others (in short, no mess). A central repository was strictly supposed to be something like a reference repository.

This requires one to use a remote repository as a central one, and initially, only Bare repositories could be used as remote repositories. With the latest changes in git, central repositories need not be bare, hence not many people know about it properly.
The only possible operations on the Bare Repository are Pushing or Cloning.
 
Using a Bare Repository
A bare repository is linked with a local repository, hence the files in .git of local repo should match with the files in the bare repo. First, create a bare repository (See section for the code snippet).
Then, create a local repository folder and clone the bare repository:

 
>cd C:/Users/example/repositories 
>git clone C:/Users/example/BareRepo.git
Cloning into 'BareRepo'...
warning: You appear to have cloned an empty repository.
done.

Don’t worry about the warning. The cloned repository will have the same name as that of the Bare Repository, navigate to that folder and add project files and commit changes. Then push the changes to the bare repository:

>git add * 
>git commit -m “First commit”
[master (root-commit) ffdf43f] First Commit
1 file changed, 1 insertion(+)
create mode 100644 example.txt
>git push c:/users/example/BareRepo.git
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 293 bytes | 97.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To c:/users/example /BareRepo.git
* [new branch] master -> master

And thus, your local repo has been linked to the Bare Repository. In case you already have some files in the project directory, directly initialize the project folder as a git repository, then push its changes to a bare repository (make sure that the Bare repository is not linked to any other project or is newly created). Another way is to clone your working project repository into a bare one:

>cd “Central Repositories” 
>git clone –bare ../../…./Default_Repo
Cloning into bare repository 'Default_Repo.git'...
done.

 
Why is only Bare Repository used as a Central Repository for syncing work?
Central Repositories use bare repositories only because git doesn’t allow you to push to a non-bare repository as the working tree will become inconsistent.
To demonstrate why you can’t push to a non-bare repository:

>cd C:/Users/example/repositories 
>mkdir RepoTest
>cd RepoTest
>git init
Initialized empty Git repository in C:/Users/example/repositories/RepoTest/.git/
>cd ../BareRepo
>git push ../RepoTest
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 293 bytes | 146.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: is denied, because it will make the index and work tree inconsistent
remote: with what you pushed, and will require 'git reset --hard' to match
remote: the work tree to HEAD.
remote:
remote: You can set the 'receive.denyCurrentBranch' configuration variable
remote: to 'ignore' or 'warn' in the remote repository to allow pushing into
remote: its current branch; however, this is not recommended unless you
remote: arranged to update its work tree to match what you pushed in some
remote: other way.
remote:
remote: To squelch this message and still keep the default behaviour, set
remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To ../RepoTest
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '../RepoTest'

But if you still want to be stubborn about it, you can read the warning and go to the non-bare repository where you wish to push and set receive.denyCurrentBranch to ignore and then push the changes.

>cd ../RepoTest 
>git config receive.denyCurrentBranch ignore
>cd ../Default_Repo
>git push ../RepoTest
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 293 bytes | 146.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ../RepoTest
* [new branch] master -> master

But, using this will give you more problems, as you keeping pushing to remote repo, you’ll notice that only the commit head points to keeps changing (along with other files in .git), but your working tree will remain the same. The only way to remove the inconsistency of index and working tree is by using the command:

>git reset –hard 

Unless you want to do this every time you push changes to the remote repository, it is recommended to use a bare repository.
A bare repository takes much less space to store the same information along with the tracked changes than a non-bare repository. Hence, its storage consumption is the most efficient. Therefore, only a bare repository is suited to serve as a remote or central repository.



Similar Reads

Git LFS: Managing Large Files in Git Repositories
Git, undoubtedly one of the most popular version control systems, is widely used for managing source code in software development projects. However, one of its limitations is its handling of large files. Traditional Git repositories struggle to efficiently manage large files, leading to bloated repositories and slow performance. This is where Git L
4 min read
Handling repositories with Git remote
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Syncing ability of the git allows the developers to access and work on any project anytime anywhere. The collaboration among developers via git takes place when git provides each individual deve
4 min read
How to Handle Big Repositories With Git?
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific application and can modify changes to it that may be
3 min read
How to Merge two Git Repositories?
Merging two Git repositories can be a complex task, especially if they have distinct histories, branches, and commit structures. This guide explores different approaches and details each step involved in merging two Git repositories. Merge One Repository as a Subtree of AnotherThis approach involves adding one repository as a subdirectory within an
1 min read
How to Remove .DS_Store Files From Git Repositories?
If you're a macOS user, you've probably encountered .DS_Store files. The macOS Finder creates these files to store custom attributes of a folder, such as the position of icons and the view settings. While they are harmless in themselves, they can clutter your Git repository and cause unnecessary conflicts when collaborating with others, especially
3 min read
How to Clone Git Repositories Including Submodules?
Git submodules are repositories embedded inside another repository. They allow you to incorporate external projects into your repository at a specific commit, giving you precise control over their versions. Cloning a repository with submodules requires a few extra steps to ensure the submodules are initialized and updated correctly. This article wi
4 min read
Managing Git Repositories with GitLab
GitLab is a powerful platform for managing Git repositories, offering a list of features that provide collaboration, code review, continuous integration/continuous deployment (CI/CD), and more. This guide will walk you through Managing Git Repositories with GitLab. Table of Content How to Access GitLab?Uploading a code file in the repositoryCreatin
3 min read
Working With Git Repositories
Git is a powerful and widely-used version control system that helps developers manage their codebases efficiently. By using Git repositories, developers can track changes, collaborate with others, and maintain a history of their project’s development. In this article, we will learn about working with Git repositories. Table of Content Types of Repo
7 min read
Difference Between Git remote prune, Git prune and Git fetch --prune
Git is a version control system that helps developers manage and track changes in their codebase. Among its many commands, git remote prune, git prune, and git fetch --prune are essential for maintaining a clean and organized repository. This article will explain the differences between these commands, their syntax, uses, and provide examples to il
4 min read
Git - git-show Command Line Utility
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific application and can modify changes to it that may be
3 min read
Git Subtree vs. Git Submodule
Git Subtree and Git Submodule are both mechanisms in Git that allow you to incorporate external repositories into your own repository. They provide a way to manage dependencies and include code from other projects while keeping your repository organized and maintainable. Git Subtree: Git Subtree allows you to insert a separate repository as a subdi
4 min read
How to Set Up Git Using Git Config?
Git is a powerful version control system that helps developers manage and track changes in their code. Setting up Git correctly is important for maintaining an organized and efficient workflow. In this article, we'll walk you through the process of setting up Git using the git config command. PrerequisitesBefore we dive into the setup process, make
3 min read
How To Exit Git Log or Git Diff?
When working with Git, the git log and git diff commands are important for viewing commit history and changes between commits, respectively. However, if you're new to Git, you might find yourself stuck in these views, unsure how to exit. This article will guide you through the steps to exit git log or git diff, ensuring a smooth experience while na
2 min read
Difference between “git add -A” and “git add”
When working with Git, the git add command is used to move changes from your working directory to the staging area. However, there are different options and flags that can be used with git add, such as -A, which might lead to some confusion. In this article, we'll explore the differences between git add -A and git add. 1. Git add- CommandThe git ad
2 min read
Git - Difference Between Git Revert, Checkout and Reset
Git offers a range of commands to manage and manipulate your codebase. Among these commands, git revert, git checkout, and git reset are frequently used for different purposes. Understanding the differences between these commands is important for effective version control. In this article, we'll explore the functionalities and use cases of git reve
6 min read
How to Fix Git Error "Unable to create '/path/my_project/.git/index.lock'"?
The Git error "Unable to create '/path/my_project/.git/index.lock'" typically occurs when Git is unable to create or write to the index.lock file. This file is used to prevent simultaneous processes from modifying the repository, which could lead to corruption. Table of Content Remove the Lock File ManuallyUse Git Command to Remove the Lock FileRes
2 min read
Difference Between "git commit" and "git push"?
Git commit and git push are two essential commands you'll use a lot when working with Git. even their frequent use together, they have different functions. In order to help you understand when and how to use these two commands effectively in your version control workflow, this article will break down their differences. What is git commit?git commit
2 min read
Difference Between Git Fetch and Git Pull
Understanding the difference between git fetch and git pull is important for effective version control in Git. Git Fetch and Git Pull are two important commands in Git that help in managing remote repositories. While both commands involve retrieving data from remote repositories, they serve distinct purposes and are used in different contexts. In t
5 min read
Git - git prune
Git prune is used to remove the unreachable objects and clear up the local repository. Git is a powerful version control system that helps developers manage their code efficiently. However, as your project evolves, you may accumulate unnecessary data that clutters your repository. This is where git prune comes in view. Table of Content What is git
6 min read
Git For Non-Developers: How Different Industries Are Adopting Git?
While Git is widely recognized for its role in software development, it's increasingly being adopted by non-developers across various industries. This shift is driven by the growing need for version control, collaboration, and efficient project management in fields like marketing, design, data science, education, and even legal and finance sectors.
8 min read
Difference Between Git Push Origin and Git Push Origin Master
Understanding the difference between git push origin and git push origin master is important for efficient version control in Git. These commands are used to upload changes from your local repository to a remote repository, but they function differently. This article will explain these differences, how each command works, and when to use them. What
3 min read
Git vs. Other Version Control Systems: Why Git Stands Out?
Version control systems (VCS) are an essential tool for developers, enabling them to track changes, collaborate on projects, and manage codebases efficiently. Among the various VCS options available, Git has emerged as the dominant choice, used by millions of developers and organizations worldwide. While other systems like Subversion (SVN), Mercuri
8 min read
Git Features
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific application and can modify changes to it which may b
9 min read
Getting changes from a Git Repository
Git allows performing various operations on the Repositories including the local repositories and remote repositories. The user when downloads a project to work upon, a local repository is created to store a copy of the original project. This local repository stores the changes that are being made by the user. This way, the user can edit the change
4 min read
Git | Working with Stash
Pre-requisites: Git Git allows multiple users to work on the same project simultaneously. Suppose a developer is working on a feature in a branch and he needs to pull changes from some other developer's branch or if he has to work urgently on some other feature, but the feature he is currently working on is incomplete. In this case, you cannot comm
8 min read
Using Git on CommandLine
Git is very important and powerful tool when it comes to the software development life cycle. Now in this article, we are going to see why git is used vastly in the software industry and what kinds of operations we can perform with git. So git is basically a distributed version control system for tracking changes in source code during software deve
5 min read
Working on BitBucket using Git
Git provides various tools and applications to perform better handling of the work environment. Git is a type of version control system that is used to allow more than one developer to access the source code of a specific application and can modify changes to it that may be seen by other developers. BitBucket is a git-based tool that helps with col
5 min read
Working on Git for GUI
Git has its native environment within the terminal. All the new features are updated first at the command line, and only there is the full power of Git. But plain text isn't the simplest choice for all tasks; sometimes some users are much more comfortable with a point-and-click interface, a visual representation is what they need. Note: There's not
4 min read
Git Rebase
Pre-requisites: Git Git rebase can integrate the changes from one branch to another by overcoming the problems that we might have faced while using the git merge command. The changes we will do will be recorded in the form of logs which are useful to go through if any mistakes happen. What is Branching?Branching means splitting from the master bran
12 min read
Recovering Lost Commits in Git
Git is a very powerful tool, and with great power comes great responsibilities. If not used and handles properly, It might cause you to lose your commits. There might be situations when you may find all of your work missing at once. If you have regularly committed your work, there is a way to recover these lost commits. This tutorial will aim at us
2 min read
Article Tags :