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)

Community
  • 1
  • 1
killjoy
  • 940
  • 1
  • 11
  • 16

1 Answers1

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(<>)'

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • I tried this, like I mentioned above. This is what happens:
    @precise32:~/gs$ git cat-file -p .git/objects/1b/2a93ec2d9f68e802d5d106983a395b368f0d36 fatal: Not a valid object name .git/objects/1b/2a93ec2d9f68e802d5d106983a395b368f0d36`
    – killjoy Apr 22 '15 at 19:41
  • just the SHA no path. git cat-file -p 1b2a93ec2d9f68e802d5d106983a395b368f0d36 – CodeWizard Apr 22 '15 at 21:04
  • ~/gs/.git/objects/a5$ ls bce3fd2565d8f458555a0c6f42d0504a848bd5
    ~/gs/.git/objects/a5$ git cat-file -p bce3fd2565d8f458555a0c6f42d0504a848bd5
    fatal: Not a valid object name bce3fd2565d8f458555a0c6f42d0504a848bd5
    – killjoy Apr 23 '15 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 Apr 23 '15 at 19:48
  • Ahh...classic noobie faux pas...thanks a bunch !! – killjoy Apr 24 '15 at 14:48