Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I unpack a single git object?

Tags:

git

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?

like image 984
Greg Bacon Avatar asked Mar 10 '16 03:03

Greg Bacon


People also ask

How do I read a Git object file?

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.

What is a blob in Git?

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.

What is Git hash object?

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.


1 Answers

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

like image 91
max630 Avatar answered Sep 23 '22 16:09

max630