What's the best way to extract a tree from a git repository?

后端 未结 3 2020
不知归路
不知归路 2020-12-06 08:15

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

相关标签:
3条回答
  • 2020-12-06 08:25

    You can use git archive for this.

    git archive master:src/ | tar -C destination -x
    
    0 讨论(0)
  • 2020-12-06 08:29

    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.

    0 讨论(0)
  • 2020-12-06 08:37

    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/
    
    0 讨论(0)
提交回复
热议问题