签出 git 存储库的子目录

Checkout subdirectory of git repository

我已经使用 post-receive hook 为 git repo 设置了 push-to-deploy,现在我需要以某种方式让它只检查特定存储库目录的内容,如下所示:

repo.git/dir1/dir11 -> /var/www/html/site1
repo.git/dir2       -> /var/www/html/site2

我只想检查目录的内容,而不是完整的结构,所以最后我会有以下内容:

/var/www/html/site1/"whatever is in dir1/dir11/ directory".

谢谢。

以这种方式为每个要维护的目标目录保留一个索引文件,并直接使用read-tree等低级命令。

# prefix the index paths with the repo's .git directory path
( export GIT_INDEX_FILE=site1.index
  export GIT_WORK_TREE=/var/www/html/site1
  git read-tree -um `git write-tree` commit:dir1/dir11
)
( export GIT_INDEX_FILE=site2.index
  export GIT_WORK_TREE=/var/www/html/site2
  git read-tree -um `git write-tree` commit:dir2
)

双树 git read-tree -um 是结帐的基础,它假设您的索引是从第一棵树派生的,并过渡到第二棵树作为结帐(或快进合并,两者执行相同的读取-tree,但合并会更新当前的 HEAD,其中 checkout 会切换它;read-tree 根本不处理 refs)。写树只会吐出你上次读树的同一棵树,或者最初是空树,除非你一直在对索引做其他事情。