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
You can use git archive
for this.
git archive master:src/ | tar -C destination -x
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.)
For a bare repository you have to pretend that a working tree exists and that you are in it:
GIT_INDEX_FILE=.tmp.index GIT_DIR=/path/to/repo.git GIT_WORK_TREE=. {
git read-tree master:src &&
git checkout-index -a --prefix=/path/to/dest/;
rm -f .tmp.index; }
If run from inside the bare repository you can omit setting GIT_DIR
.
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/