tags:

views:

78

answers:

3

Extracting a blob (file) from an arbitrary revision is easy with git show, e.g.:

 git show master:src/hello-world.c > /tmp/hello.c

However, I'd like to know if is there a similar way in git to extract a tree (directory) and everything under it recursively?

I've written a small script that does this, so I'll add that as a possible answer. It seems that this is the kind of thing that may well be built in to git, but I just don't know how to find it...

+5  A: 

You can use git archive for this.

git archive master:src/ | tar -C destination -x
Charles Bailey
Goodness, an instant answer! Thanks, that's ideal - much faster than my script.
Mark Longair
An added bonus to this is that you can do it remotely, against another (even bare) repository with --remote
cxreg
A: 

I'm doing this at the moment with this script:

http://gist.github.com/498447

... which parses the output of git ls-tree -r -z <tree-ish>. You can pass it anything that git ls-tree can understand as a tree, e.g:

extract-tree-from-git.py master:src/tests/ /tmp/extracted-tests/
extract-tree-from-git.py HEAD^ /tmp/parent-of-head/
Mark Longair
+3  A: 

You can use read-tree and checkout-index with a temporary index file:

GIT_INDEX_FILE=.tmp.index { git read-tree master:src &&
                            git checkout-index -a --prefix=dest/; 
                            rm -f .tmp.index; }

(Line breaks added for clarity, but it's really a one-liner.)

Charles Bailey
Clever :) Is there any particular reason to prefer that answer to your `git archive` answer, other than not depending on tar?
Mark Longair
@Mark Longair: This one's more 'low level'; the archive solution is more of a user command. I've no idea if this one performs better or worse. I kept the answers separate as I'm genuinely interested to know which one is more popular.
Charles Bailey
As cxreg alludes to, with a bare repository and git 1.7.0.4 the "git checkout-index" step fails with "fatal: This operation must be run in a work tree". (This seems somewhat strange in the case where GIT_INDEX_FILE is set and --prefix is used, I think.) So I'll accept the other answer, since the bare repository case is important for me, but this very useful to know about - thanks for both.
Mark Longair