format-pack Git Command Guide
The git format-pack format stores Git objects in compressed delta-encoded archives to optimize storage space, network transfer, and repository performance. Pack files are the primary storage mechanism for Git repositories.
Pack Format Specification
Section titled “Pack Format Specification”Pack File Header:
Section titled “Pack File Header:”| Field | Size | Description |
|---|---|---|
| Signature | 4 bytes | ’P’, ‘A’, ‘C’, ‘K’ |
| Version | 4 bytes | Version 2 or 3 (network byte order) |
| Object Count | 4 bytes | Number of objects in pack (network byte order) |
Object Entry Format:
Section titled “Object Entry Format:”Undeltified Objects:
- type and length (3-bit type, (n-1)*7+4-bit length)
- compressed object data
Delta Objects:
- type and length (3-bit type, (n-1)*7+4-bit length)
- base object name (SHA-1 for OBJ_REF_DELTA)
- compressed delta data
Delta Types:
Section titled “Delta Types:”| Type Value | Name | Base Reference |
|---|---|---|
| 6 (0b110) | OBJ_REF_DELTA | SHA-1 hash of base object |
| 7 (0b111) | OBJ_OFS_DELTA | Negative offset to base object in pack |
Object Types:
Section titled “Object Types:”| Type Value | Name | Description |
|---|---|---|
| 1 | OBJ_COMMIT | Commit object |
| 2 | OBJ_TREE | Tree object |
| 3 | OBJ_BLOB | Blob (file content) object |
| 4 | OBJ_TAG | Tag object |
Examples of git pack operations:
Section titled “Examples of git pack operations:”Inspect pack files
Section titled “Inspect pack files”ls -la .git/objects/pack/List all pack files in the repository.
Verify pack integrity
Section titled “Verify pack integrity”git verify-pack .git/objects/pack/pack-*.idxCheck pack file integrity and show object statistics.
Create custom pack
Section titled “Create custom pack”git pack-objects custom-pack < object-list.txtCreate a custom pack file from specified objects.
Repack repository
Section titled “Repack repository”git repack -a -dRepack all objects into a single pack, removing old packs.
Unpack objects
Section titled “Unpack objects”find .git/objects/pack -name "*.pack" -exec git unpack-objects {} +Unpack objects from pack files to loose objects.
Verify all packs
Section titled “Verify all packs”git for-each-ref --format='%(objectname)' | git pack-objects verify-packVerify packs contain expected objects.
Create thin pack
Section titled “Create thin pack”git pack-objects --thin thin-pack < object-list.txtCreate thin pack that references objects not in the pack.
How does Git decide when to create pack files?
Section titled “How does Git decide when to create pack files?”Git creates pack files during git gc, git repack, or when the number of loose objects exceeds gc.autoPackLimit (default 50).
What types of objects can be packed?
Section titled “What types of objects can be packed?”All Git objects (commits, trees, blobs, tags) can be packed using delta compression to save space.
What’s the difference between thin and normal packs?
Section titled “What’s the difference between thin and normal packs?”Thin packs reference base objects not included in the pack, allowing smaller transfers at the cost of requiring access to referenced objects.
How do I inspect what’s in a pack file?
Section titled “How do I inspect what’s in a pack file?”Use git verify-pack -v .git/objects/pack/pack-*.idx to see all objects, their sizes, and delta chains.
How do I recover from a corrupted pack file?
Section titled “How do I recover from a corrupted pack file?”For corrupted packs, try git fsck --full first. If recovery fails, use backups or attempt unpacking with git unpack-objects < packfile.
What affects pack compression size?
Section titled “What affects pack compression size?”Compression depends on similarity between objects, Git version pack compression algorithm settings, and whether deltas are enabled.
Why do some objects remain loose?
Section titled “Why do some objects remain loose?”Recent objects stay loose for performance. Only gc/periodic maintenance consolidates them into packs.
Applications of the pack format
Section titled “Applications of the pack format”- Storage Efficiency: Compresses thousands of objects into single files with delta compression
- Memory Mapping: Allows efficient memory mapping of object data during operations
- Network Efficiency: Enables fast push/pull operations with thin pack optimization
- Clone Performance: Speeds up initial cloning by bundling repository data
- Backup Strategy: Facilitates repository backups and archival
- Scalability: Supports repositories with millions of objects through multi-pack-index