Git index-pack: Build and Verify Pack Indexes
Understand how git index-pack builds pack indexes for existing Git packfiles, handles standard input, reverse indexes, strict validation, and keep files without confusing index construction with object repacking.
Advanced
What a pack index is for
Git can store many objects together in a packfile. The corresponding `.idx` file provides the index needed to work with objects in that packed archive efficiently. `git index-pack` takes an existing pack and builds its index; it does not create a new history or perform the repository-wide repacking job handled by `git repack`.
git repack -ad
find .git/objects/pack -maxdepth 1 -type f -printf '%f\n' | sortQuick check
What is the primary artifact produced by `git index-pack` for an existing `.pack` file?
Index an existing pack
With a pack path ending in `.pack`, the basic form builds an index whose default name replaces `.pack` with `.idx`. The command prints the resulting pack object ID. If a reverse index is requested, Git can also write the corresponding `.rev` file.
git index-pack .git/objects/pack/pack-<id>.pack
git index-pack --rev-index .git/objects/pack/pack-<id>.packQuick check
What does `--rev-index` request?
Choose the index destination
`-o <index-file>` writes the generated index to the path you specify instead of deriving the index name from the pack filename. This is useful when a pack is being handled outside its final repository location.
cp .git/objects/pack/pack-<id>.pack /tmp/sample.pack
git index-pack -o /tmp/sample.idx /tmp/sample.pack
ls -l /tmp/sample.pack /tmp/sample.idxQuick check
Why use `-o`?
Read a pack from standard input
`--stdin` changes the input path: the pack is read from standard input. Git can then write the received pack to a destination pack file and build its index. When a pack is arriving through a stream, `--keep` can create a `.keep` file so an overlapping `git repack` does not remove the new pack before refs are updated.
git index-pack --stdin --keep='incoming pack' -o /tmp/incoming.idx /tmp/incoming.pack < .git/objects/pack/pack-<id>.pack
ls -l /tmp/incoming.pack /tmp/incoming.idx /tmp/incoming.keepQuick check
What is the purpose of `--keep` in an incoming-pack workflow?
Validate while indexing
`--strict` makes index-pack fail when the pack contains broken objects or links. It is different from `git verify-pack`: index-pack can validate while constructing the index, whereas verify-pack is a separate operation for an already indexed pack.
git index-pack --strict .git/objects/pack/pack-<id>.pack
git verify-pack -v .git/objects/pack/pack-<id>.idxQuick check
Which command is specifically the separate post-indexing pack verification operation?
Thin packs and `--fix-thin`
A thin pack can omit objects that its deltas depend on when those bases are known to exist at the receiving side. `--fix-thin` tells index-pack to add the excluded base objects when processing such a pack. The option only makes sense with `--stdin`, and it belongs to the low-level pack-transfer machinery rather than ordinary repository maintenance.
git index-pack --stdin --fix-thin --keep < incoming.packQuick check
When does `--fix-thin` make sense?
Do not confuse pack indexing with repacking
`git index-pack` consumes an existing pack and builds the index needed for it. `git repack` is the repository-level operation that creates and reorganizes object packfiles. `git verify-pack` validates an existing pack/index pair. `git multi-pack-index` indexes multiple packs together. These commands are related, but they own different storage layers and workflows.
git repack -ad
git index-pack .git/objects/pack/pack-<id>.pack
git verify-pack -v .git/objects/pack/pack-<id>.idx
git multi-pack-index verifyPractice what you learned
Use the dedicated Git code-completion exercise for this lesson.
Thanks for your feedback.
Mark this learning resource complete to track your learning progress.