What is a Git Repository?
A Git repository is a database that stores the complete history of your project, including all files, directories, commits, branches, and metadata. It’s the foundation of Git’s distributed version control system, enabling you to track changes, collaborate with others, and maintain a full record of your project’s evolution.Every Git repository is self-contained and stores the entire project history locally, making Git a truly distributed version control system.
Repository Types
Git supports two main types of repositories:A standard repository with a
.git directory at the root of your working tree. This is what you typically create when starting a new project:.git directory containing all Git internalsA bare repository (typically named
<project>.git) contains only the Git data without a working tree. These are commonly used as central repositories for collaboration:Repository Structure
Inside the.git directory, Git maintains a well-defined structure:
Core Directory Structure
Core Directory Structure
objects/
The object database stores all content: commits, trees (directories), blobs (files), and tag objects. Objects are identified by their SHA-1 hash.refs/
Stores references (pointers to commits):refs/heads/- Local branchesrefs/tags/- Tagsrefs/remotes/- Remote-tracking branches
HEAD
A symbolic reference pointing to your current branch:index
The staging area (covered in detail in the Staging Area concept). A binary file tracking what will go into your next commit.config
Repository-specific configuration settings, including:- Remote repository URLs
- Branch tracking information
- User preferences for this repository
hooks/
Customization scripts that run at specific points in Git’s execution (e.g.,pre-commit, post-merge).The Object Database
Git’s object database is implemented inobject-file.c and uses a content-addressable storage system. Every object has:
- An ID - A 40-character SHA-1 hash of the object’s type and contents
- A type - One of: commit, tree, blob, or tag
- Contents - The actual data
Object Storage Formats
Loose objects: Newly created objects are stored individually:Repository Layout Example
Here’s what a typical repository structure looks like:Repository Operations
Creating a Repository
Repository Discovery
Git searches for a repository by looking for a.git directory in the current directory and then each parent directory. This is implemented in setup.c:
Gitfiles and Worktrees
Git supports a special mechanism called gitfiles where.git is a plain text file instead of a directory:
- Submodules - To allow the parent repository to remove submodule working trees without losing the repository
- Worktrees - To enable multiple working directories sharing one repository
Object Reachability and Garbage Collection
Git only keeps objects that are reachable from:- References (branches, tags)
- The reflog
- The index
git gc (garbage collection):
Repository Configuration
Repositories have three configuration levels:- System (
/etc/gitconfig) - Applies to all users - Global (
~/.gitconfig) - User-specific settings - Local (
.git/config) - Repository-specific settings
Key Implementation Details
Fromrepository.h and repository.c:
- Repository struct - Core data structure managing repository state
- Object database - Content-addressable storage with SHA-1 addressing
- Reference storage - Multiple backends (files, reftable) for storing refs
- Work tree - Association between repository and working directory
Best Practices
Keep repositories focused
One repository per project or logical unit. Avoid creating mega-repositories unless using advanced features like sparse checkout.
Don't commit build artifacts
Use
.gitignore to exclude generated files, dependencies, and build outputs from the repository.Use bare repositories for sharing
When setting up a central repository, use
--bare to prevent direct editing conflicts.Related Concepts
- Commits - Snapshots stored in the repository
- Branches - Named pointers to commits
- Staging Area - Preparing commits
- Remote Repositories - Collaborating with others
Further Reading
git help repository-layout- Complete repository structure referencegit help config- Configuration system documentationgit help gc- Garbage collection and repository maintenance
