In an answer to a question about the internal format of git’s tree objects, to get to one particular tree object that was stored in a pack, I unpacked all of the objects in a repository’s packfiles using git unpack-objects
. That seems like overkill. Yes, I realize that commands such git show --raw
, git ls-tree
, or git cat-file
will get close to the internal format.
How do I make git output a single object in its internal format — complete with blob
, tree
, commit
, or tag
header — regardless of whether it is stored in loose or packed format?
git will read the files for you with git show or git cat-file . When a repository gets larger, git may use another, less simple format to store the data, called packfiles, in which case these simple code fragments won't work. The fragments are to show the simplicity of the default way that git stores objects.
A Git blob (binary large object) is the object type used to store the contents of each file in a repository. The file's SHA-1 hash is computed and stored in the blob object. These endpoints allow you to read and write blob objects to your Git database on GitHub.
DESCRIPTION. Computes the object ID value for an object with specified type with the contents of the named file (which can be outside of the work tree), and optionally writes the resulting object into the object database. Reports its object ID to its standard output.
You can use git pack-objects first to make a pack with only this one object.
Note also that git unpack-objects does not extract objects which already exist in a repository, so you probably want to make a new repository. Overall something like this should work:
$ DIR=$(mktemp -d)
$ echo 5e98806c6cc246acef5f539ae191710a0c06ad3f \
| git pack-objects --stdout \
| ( cd "$DIR" && git init && git unpack-objects )
Then look for your unpacked object in $DIR/.git/objects
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With