Local Mirrors and Caching
When working with large repositories or in environments with limited network access, Git provides several ways to reduce redundant data transfers and speed up operations using mirrors and caching.
1. Local Git Mirrors
A mirror is a bare clone of a repository that includes all refs and remote branches.
- Acts as a fast, up-to-date local source.
- Commonly used as a shared reference for teams or CI systems.
Command:
git clone --mirror <repo-url>
This creates a .git directory with all objects and refs. You can update it periodically:
cd repo.git
git remote update
2. Reference Repositories
Reference repositories let you clone new repositories by reusing an existing mirror as the source of Git objects.
- Saves bandwidth and speeds up clones.
- The new repo still gets its own
.gitdirectory.
Command:
git clone --reference /path/to/local/mirror <repo-url>
3. Using Alternates for Shared Object Storage
You can set up multiple repositories to use a common object directory using the alternates mechanism.
- Located at
.git/objects/info/alternates - Lets repositories share objects instead of duplicating them.
Example:
echo /opt/shared-objects/.git/objects > .git/objects/info/alternates
This works best in environments where repositories are read-only or periodically synced.
4. Caching Servers (Advanced)
In CI/CD or enterprise environments, you can deploy caching servers that act like local mirrors for Git objects.
- Reduces load on public remotes.
- Git will fetch missing objects from the cache first.
Tools like git-cache-http-server or internal proxies can be used.
Summary
| Technique | Purpose | Benefit |
|---|---|---|
| Git Mirror | Clone all refs & branches | Fast local backup/reference |
| Reference Clone | Use existing repo objects | Faster and bandwidth-efficient clones |
| Alternates Mechanism | Share object directory | Reduced storage, faster initialization |
| Caching Server | Serve objects within a network | CI/CD scaling and reduced network load |