What is the difference between git init and git init --bare?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
git init creates a working repository where you edit files and make commits. git init --bare creates a bare repository that stores only Git's internal data (commits, branches, objects) without a working directory. Bare repositories are used as shared remote servers (like GitHub). You push to them and pull from them but never edit files directly in them. Working repositories are where you do your actual development.
git init (Working Repository)
This creates:
You work directly in my-project/, editing files, staging with git add, and committing with git commit. The .git/ subdirectory stores all version history.
git init --bare (Bare Repository)
This creates:
No working directory, no .git/ subdirectory. The Git data lives at the top level. By convention, bare repositories are named with a .git suffix.
Key Differences
| Feature | git init | git init --bare |
| Working directory | Yes (edit files) | No |
.git/ subdirectory | Yes | No (Git data is at root) |
Can git add/git commit | Yes | No |
Accepts git push | Not recommended | Yes (designed for this) |
| Use case | Development | Shared remote/server |
| Convention | my-project/ | my-project.git/ |
Why Not Push to a Non-Bare Repository?
Git warns you about this:
Bare repositories avoid this problem by having no working directory to get out of sync.
Setting Up a Shared Repository
This is exactly what GitHub, GitLab, and Bitbucket do. They host bare repositories.
Converting Between Types
You cannot directly convert a working repo to bare by deleting the working files. Use git clone --bare to create a proper bare copy.
Bare Repository Configuration
Hooks in Bare Repositories
Bare repos support server-side hooks:
post-receive runs after a push is accepted. This is how many deployment pipelines work.
Mirror Repository
--mirror creates a bare clone that includes all refs (branches, tags, notes). Used for backups and migrations.
Multiple Remotes Workflow
Common Pitfalls
- Pushing to non-bare repos: Git refuses pushes to the currently checked-out branch of a working repository. Use a bare repo as the remote, or configure
receive.denyCurrentBranch(not recommended). - Editing files in bare repos: There are no working files to edit. To change content, clone the bare repo, edit in the clone, and push back.
- Forgetting the
.gitsuffix: Convention is to name bare repos with.git(e.g.,project.git). This is not required but helps distinguish bare from working repos. - Permissions: On shared servers, set
git init --bare --shared=groupto allow multiple users to push. Without--shared, filesystem permissions may block other users. - Shallow clones from bare repos:
git clone --depth 1from a bare repo works but the bare repo must have the full history. You cannot create a "shallow bare" repo.
Summary
git initcreates a working repository for development (edit, add, commit)git init --barecreates a server repository for sharing (push, pull, no working files)- Bare repos have no working directory. Git data lives at the top level
- Never push to a non-bare repository's checked-out branch
- Use bare repos for shared remotes, CI/CD, and deployment hooks
- GitHub/GitLab/Bitbucket all host bare repositories internally
Related reading
- What is the difference between 'git pull' and 'git fetch'?
- What is the difference between git pull and git fetch git rebase?
- What is the difference between GitHub and gist?
- What is the difference between HEAD and HEAD in Git?
- What is the difference between join and merge in Pandas?
- What is the Difference Between Mercurial and Git?
- What is the difference between merge --squash and rebase?
- What is the difference between origin and upstream on GitHub?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.