252

When I open the Git GUI, I get a popup message that refers to loose objects. I did git gc and that removed the message.

What are loose objects and how could I prevent this from occurring again?

gturri
  • 13,807
  • 9
  • 40
  • 57
oxo
  • 4,343
  • 10
  • 32
  • 35
  • 1
    You can simply click **"Yes"** in the popup message when it asks if the loose objects should be packed now. *Git GUI* will run `git gc` for you, with a nice GUI progress bar (albeit shown as stuck most of the time). – ADTC Aug 27 '14 at 07:50

2 Answers2

168

An object (blobs, trees, and commits) with SHA say - 810cae53e0f622d6804f063c04a83dbc3a11b7ca will be stored at

.git/objects/81/0cae53e0f622d6804f063c04a83dbc3a11b7ca

( the split in first two characters to improve performance of the File system as now not all the objects are stored in the same directory)

Objects stored as above are referred to as Loose objects.

When you start up with your repo, you mostly have loose objects. As the number goes high, it becomes inefficient and they are stored in a pack file. Such objects are called packed objects.

 git gc

is what you run to pack objects (Usually loose objects that are not needed and few weeks old are also removed and with --prune=<date> option you can force remove loose objects that are no longer needed. Like when you amend a commit. The old commit object is no longer needed. )

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 4
    The `--prune` option is enabled by default, and since `git gc` is automatically triggered by common usage (e.g. `commit`), you usually don't have to worry about this. I don't use git gui, and I can't find exactly where it's triggered in the source, but either it does its own check, or just intercepts the `gc` triggered by a called command. It's certainly nothing to worry about, though, just caused by normal usage. – Cascabel Apr 19 '11 at 02:22
  • 33
    is there any downside to packing loose objects? If not, why doesn't Git do it automatically? – Louis Rhys Apr 24 '13 at 08:37
  • 15
    I don't think it is true that it happens automatically. I commit often but 'git gui' mentioned had 50,000 loose objects (and have been wondering why git was so damn slow, large project over ~4 years with no manual gc) – Kevin May 09 '13 at 14:20
  • 1
    @Kevin Did the performance of your large project improve after running `git gc` manually? I think it should improve, because loose objects are inefficient, and 50,000 is a very large number. I also found out that packing greatly reduces the space used by the `.git` folder as well. – ADTC Aug 27 '14 at 07:47
  • 5
    @LouisRhys, "When objects are written to disk, it is often in the loose format, since that format is less expensive to access. However, eventually you'll want to save the space by packing up the objects" -- from Git Book link in [answer below](http://stackoverflow.com/a/5709910/1003959) – chris Apr 18 '15 at 19:49
  • Does the packing operation in `git gc` also perform binary delta encoding for large binary files? – CMCDragonkai May 16 '16 at 09:34
  • 3
    Is this an operation that affects the remote repo? Or is it only a local optimization? – Xriuk Sep 07 '21 at 16:08
43

The Git Book explains it pretty well: https://git-scm.com/book/en/v2/Git-Internals-Packfiles

Loose objects are the simpler format. It is simply the compressed data stored in a single file on disk. Every object written to a seperate file.

MEmerson
  • 772
  • 1
  • 6
  • 17
Mohamed Mansour
  • 39,445
  • 10
  • 116
  • 90