通过 --work-tree 在别处获取 git 存储库的树,同时保持 --git-dir 存储库不变

Obtain a git repository's tree elsewhere via --work-tree while keeping --git-dir repository unmodified

根据 /orig-git 命令

给出一个签出的存储库
git --git-dir=/orig-git/.git --work-tree=/copy checkout -f REV

导致 /copy/ 中的检出树表示 REV 处的状态,但 修改 /orig-git 存储库以指向 REV(同时保持文件本身不变)。是否可以仅在 /copy 中获取修订版 REV 的副本,而无需通过克隆、获取或只是简单地复制此 .git 存储库来复制 /orig-git/.git 中的信息?

基本上我想对 git 回购信息进行只读操作,但仍然 modify/write 其他地方的结帐树。

似乎最简单的选择就是克隆存储库:

git clone -l /orig/git /copy

现在您拥有了一个存储库副本,您可以在不影响原始版本的情况下随意修改它。如果您真的想用 /copy 中的更改更新原始文件,如果 /orig 配置正确,您可以 git push 将它们返回。

如果您担心 space 被存储库的第二个副本使用,-l 标志意味着只要有可能,"files under the .git/objects/ directory are hardlinked to save space when possible"(来自 git-clone(1)) .

直接来自git-archive

git archive --format=tar --prefix=junk/ HEAD | (cd /var/tmp/ && tar xf -)

Create a tar archive that contains the contents of the latest commit on the current branch, and extract it in the /var/tmp/junk directory.

只需将 HEAD 替换为您感兴趣的提交 (REV)。