1

I'm writing a program that needs to concurrently extract different commits from the same repository (like in this question, but concurrently).

Porcelain commands such as git checkout and git archive operate on the index, thus any time a commit would need to be extracted, it would need to move HEAD and/or go through the index first.

Is there a simple way to just dump a Git tree object into a directory on the filesystem?

Community
  • 1
  • 1
Vladimir Panteleev
  • 24,651
  • 6
  • 70
  • 114
  • 4
    `git archive` does not operate on the index, and does not require modifications to HEAD, index or working directory. `git archive -o /tmp/somefile.tar sha1-of-old-commit` is probably what you want. A tag, branch name, or other form of ref (e.g. `dev@{3 days ago}`) is equally appropriate for the sha1 argument. – twalberg May 13 '15 at 19:33

2 Answers2

0

One way to do it programmatically is to use git cat-file.

When started in batch mode (--batch), you can feed commit IDs to its standard input, and it will immediately dump the object to standard output (incl. a header to indicate the object type and length). You can thus traverse the commit's tree object recursively by spawning a git cat-file process, writing object (blob and sub-tree) SHAs to its standard input, writing blobs to disk, and recursing for tree objects.

Note that the tree objects will be dumped in binary format; you can find a description of the format in this answer.

Community
  • 1
  • 1
Vladimir Panteleev
  • 24,651
  • 6
  • 70
  • 114
0

You can use git archive to export a tar archive of a particular commit, and immediately extract it to some target directory:

git archive <commit> | tar -xf - -C <output-directory>

This will also work for any tree object, instead of <commit>.

frasertweedale
  • 5,424
  • 3
  • 26
  • 38