2

When Git stores a snapshot of a file, it stores an object called a blob.

This is compressed using zlib..see http://nfarina.com/post/9868516270/git-is-simpler; either I am blind or he doesn't explain how the file blob was uncompressed in the fist place (while everything else was explained as if to a noob)

How do we extract this and view the contents of a blob ? Most of the results from googling this applied to uncompressing within scripts/programs, not manual/CL:

Deflate command line tool, https://unix.stackexchange.com/q/22834

I was looking for a single one-line command line way to do this on a single file.

Thanks in advance!

(Even if this question sounds like a duplicate, the barrage of answers in the other link are no way as accurate as the one here. I think this thread should be kept alive or this answer be posted there to help others with a non-convoluted way to deflate)

1
  • "openssl zlib -d" does what was asked for: uncompressing of zlib-file via command line. Commented Jan 28, 2021 at 17:05

1 Answer 1

5

use git cat-file -p SHA1 to view the file content of the blob.

The content of the file is this: blob XXX NULL Content

ex:

a.txt contain Hello World

The Content of the SHA-1 is:

blob 11\000Hello World and this strign is then GZipped

If you are on unix u can use this to deflate the content:
perl -MCompress::Zlib -e 'undef $/; print uncompress(<>)'

4
  • I tried this, like I mentioned above. This is what happens: <br/> @precise32:~/gs$ git cat-file -p .git/objects/1b/2a93ec2d9f68e802d5d106983a395b368f0d36 fatal: Not a valid object name .git/objects/1b/2a93ec2d9f68e802d5d106983a395b368f0d36`
    – killjoy
    Commented Apr 22, 2015 at 19:41
  • just the SHA no path. git cat-file -p 1b2a93ec2d9f68e802d5d106983a395b368f0d36
    – CodeWizard
    Commented Apr 22, 2015 at 21:04
  • <br/> ~/gs/.git/objects/a5$ ls bce3fd2565d8f458555a0c6f42d0504a848bd5 <br/> ~/gs/.git/objects/a5$ git cat-file -p bce3fd2565d8f458555a0c6f42d0504a848bd5 <br/> fatal: Not a valid object name bce3fd2565d8f458555a0c6f42d0504a848bd5
    – killjoy
    Commented Apr 23, 2015 at 19:19
  • you need 40 characters. 2 are the folder name and 38 are the file name if you take it from the .git folder. If you have the id from git ls-tree -s then you already have the full id.
    – CodeWizard
    Commented Apr 23, 2015 at 19:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.