How to Create a Bare Repository in Git: Simple Steps
Use the command
git init --bare to create a bare repository in Git. This type of repository has no working files and is used mainly for sharing code between users.Syntax
The basic syntax to create a bare repository is:
git init --bare [repository-name].git
Here, --bare tells Git to create a repository without a working directory, and [repository-name].git is the folder name for the bare repo.
bash
git init --bare myproject.git
Example
This example shows how to create a bare repository named project.git and then clone it to work on files.
bash
mkdir project.git cd project.git git init --bare cd .. git clone project.git project-working cd project-working echo "Hello Git" > README.md git add README.md git commit -m "Add README" git push origin main
Output
Initialized empty Git repository in /path/project.git/
Cloning into 'project-working'...
[main (root-commit) 1a2b3c4] Add README
1 file changed, 1 insertion(+)
To project.git
0000000..1a2b3c4 main -> main
Common Pitfalls
Common mistakes when creating bare repositories include:
- Forgetting the
--bareflag, which creates a normal repository with a working directory instead of a bare one. - Using a bare repository as a working directory, which is not possible because it has no checked-out files.
- Not naming the bare repository folder with a
.gitsuffix, which is a common convention to identify bare repos.
bash
git init myrepo # This creates a non-bare repo # Correct way: git init --bare myrepo.git
Quick Reference
Summary tips for bare repositories:
- Use
git init --bareto create a bare repo. - Bare repos are for sharing and do not have working files.
- Name bare repos with a
.gitsuffix for clarity. - Clone bare repos to work on files locally.
Key Takeaways
Create a bare repository using
git init --bare to share code without a working directory.Bare repositories do not contain checked-out files and are used as central repositories.
Always name bare repositories with a
.git suffix to follow conventions.Clone bare repositories to get a working copy for editing and committing files.
Avoid using bare repositories as working directories since they lack files to edit directly.