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
1. Non-bare Repository (Default Repository)
A non-bare repository is the standard Git repository you get when you run git init in a project folder. It contains:
- A
.git folder (stores all information about commits, branches, and history) - Your project files (working tree)
File structure of a non-bare repository:
-- Default_Repo*
|-- .git*
| |-- hooks*
| |-- info*
| |-- logs*
| |-- objects*
| |-- refs*
| |-- COMMIT_EDITMSG
| |-- config
| |-- description
| |-- HEAD
| |-- index
|-- example.txt
*: Folders
Key Points:
- Used for local development
- You can edit, commit, and track changes
.git folder is the “brain” of your repository
2. Bare Repository
A bare repository is different because it does not have a working tree, meaning you cannot directly modify or commit files inside it. It contains only the .git folder contents.
Why does it exist?
- Serves as a central repository for collaboration
- Acts as a “reference” for other developers to clone, push, or pull changes
- Prevents conflicts in shared development environments
Creating a Bare Repository:
>mkdir FileName.git >cd FileName.git >git init –bare
File structure of a bare repository:
-- BareRepo.git*
|-- hooks*
|-- info*
|-- logs*
|-- objects*
|-- refs*
|-- COMMIT_EDITMSG
|-- config
|-- description
|-- HEAD
|-- index
*: Folders
Key Points:
- Has the same structure as
.git folder in non-bare repo. - Always named with
.git extension (e.g., BareRepo.git). - Cannot commit changes directly.
- Only operations: push or clone.
3. Using a Bare Repository
Step 1: Clone a bare repository
Create a local repository by cloning the bare repository:
cd C:/Users/example/repositories
git clone C:/Users/example/BareRepo.git
Output might show a warning:
warning: You appear to have cloned an empty repository.
Step 2: Add files and commit locally
cd BareRepo
git add *
git commit -m "First commit"
Step 3: Push changes to the bare repository
git push C:/Users/example/BareRepo.git
Now, your local repository is linked to the bare repository.
4. Converting a Local Repository to Bare
If you already have a local repository, you can clone it as bare:
cd "Central Repositories"
git clone --bare ../../path_to_local_repo
This creates LocalRepo.git as a bare repository.
5. Why Only Bare Repositories Are Used as Central Repositories
Git does not allow pushing to a non-bare repository by default.
- If you try, you’ll get an error like:
remote: error: refusing to update checked out branch: refs/heads/master
You can bypass it by changing settings (receive .deny Current Branch ignore), but this can create inconsistencies between the working tree and commits.
Key Reasons to use Bare Repositories as Central Repositories:
- Avoids conflicts between multiple developers
- Prevents inconsistent working tree
- Efficient storage (only tracks
.git contents) - Recommended for remote servers
Non-bare Repository (Local) Vs Bare Repository (Central/Remote)
| Feature | Non-bare Repository (Local) | Bare Repository (Central/Remote) |
|---|
| Working Tree | Yes | No |
| Commit Files Directly | Yes | No |
| Purpose / Use | Local development | Central repository / collaboration |
| File Structure | .git + project files | Only .git contents |
| Push / Pull | Pull from remote, push to bare | Accepts push and allows clone only |
What is the main difference between a non-bare repository and a bare repository?
-
A bare repository contains both .git data and working files
-
A non-bare repository does not have a .git folder
-
A non-bare repository includes a working tree, while a bare repository does not
-
A bare repository allows direct editing of project files
Explanation:
A non-bare repository has both the .git folder and the working tree (actual project files). A bare repository contains only the .git data and no working tree, meaning you cannot directly edit or commit files inside it. Bare repositories act as central servers for collaboration.
Why are bare repositories used as central repositories in Git?
-
They allow developers to edit files directly
-
Git does not allow pushing to non-bare repositories by default
-
They store more history than non-bare repositories
-
They automatically merge all branches
Explanation:
Git prevents pushing to a non-bare repository because it may have an active working tree, leading to inconsistencies. Bare repositories avoid such conflicts because they contain no working files. Therefore, they are the recommended choice for central servers.
Which of the following operations can be performed directly inside a bare repository?
-
-
-
Pushing from local repos or cloning
-
Explanation:
Bare repositories cannot be used for editing, staging, or committing, since there is no working tree. They only accept Git operations like push, pull, and clone, which makes them ideal for centralized collaboration.
What command is used to create a bare version of an existing local repository?
Explanation:
Using git clone --bare converts a local repository into a bare version. This creates a new repository that contains only .git contents and is typically named with a .git extension, such as MyRepo.git.
What happens if multiple developers push to a non-bare repository with a working tree?
-
Git automatically resolves conflicts
-
It works normally with no issues
-
The working tree may become inconsistent, causing errors
-
The repository becomes read-only
Explanation:
A non-bare repository has an active working tree, so allowing pushes can lead to inconsistent states, overwriting files that are currently checked out. Git prevents this to ensure safety, which is why only bare repositories should be used as shared central repositories.
Quiz Completed Successfully
Your Score : 2/5
Accuracy : 0%
Login to View Explanation
1/5
1/5
< Previous
Next >
Explore
Git Introduction
Git Installation and Setup
All Git Commands
Most Used Git Commands
Git Branch
Git Merge
Git Tools and Integration
Git Remote Repositories
Collaborating with Git
Advanced Git Commands