Earlier, we saw that one file under .git/objects/??/
directory is
stored for each Git object you create. This representation is
efficient to create atomically and safely, but not so convenient
to transport over the network. Since Git objects are immutable
once they are created, there is a way to optimize the storage by
"packing them together". The command
$ git repack
will do it for you. If you followed the tutorial examples, you
would have accumulated about 17 objects in .git/objects/??/
directories by now. git repack tells you how many objects it
packed, and stores the packed file in the .git/objects/pack
directory.
Note
You will see two files, pack-*.pack
and pack-*.idx
, in
.git/objects/pack
directory. They are closely related to each
other, and if you ever copy them by hand to a different
repository for whatever reason, you should make sure you copy
them together. The former holds all the data from the objects
in the pack, and the latter holds the index for random
access.
If you are paranoid, running git verify-pack command would detect
if you have a corrupt pack, but do not worry too much. Our
programs are always perfect ;-).
Once you have packed objects, you do not need to leave the
unpacked objects that are contained in the pack file anymore.
$ git prune-packed
would remove them for you.
You can try running find .git/objects -type f
before and after
you run git prune-packed
if you are curious. Also git
count-objects
would tell you how many unpacked objects are in
your repository and how much space they are consuming.
Note
git pull
is slightly cumbersome for HTTP transport, as a
packed repository may contain relatively few objects in a
relatively large pack. If you expect many HTTP pulls from
your public repository you might want to repack & prune
often, or never.
If you run git repack
again at this point, it will say "Nothing
new to pack.". Once you continue your development and accumulate
the changes, running git repack
again will create a new pack,
that contains objects created since you packed your repository
the last time. We recommend that you pack your project soon after
the initial import (unless you are starting your project from
scratch), and then run git repack
every once in a while,
depending on how active your project is.
When a repository is synchronized via git push
and git pull
objects packed in the source repository are usually stored
unpacked in the destination. While this allows you to use
different packing strategies on both ends, it also means you may
need to repack both repositories every once in a while.