8

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?

3
  • 2
    You can just zlib decompress it with zpipe -d < <object> from within .git/objects
    – Andrew C
    Commented Mar 10, 2016 at 4:30
  • @AndrewC Yes, if it’s already a loose object, but what if it lives in a pack?
    – Greg Bacon
    Commented Mar 10, 2016 at 5:06
  • Greg I thought the premise of your question was that you had unpacked the objects and had a loose object. I don't know the answer to doing it from a pack file except to unpack it first.
    – Andrew C
    Commented Mar 10, 2016 at 5:12

1 Answer 1

8

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

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