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

后端 未结 3 2025
不知归路
不知归路 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: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.

提交回复
热议问题