我可以从本地 git 镜像克隆中检出分支吗

Can I checkout branch from local git mirror clone

我已经使用 --mirror 标志克隆了我的存储库,如果可以,我可以从中检出分支吗?如何?如果没有,为什么?

git branch 显示所有分支的列表,但 git checkout branch-name 给我错误提示:
fatal: This operation must be run in a work tree

参考官方docs:

--mirror

Set up a mirror of the source repository. This implies --bare. Compared to --bare, --mirror not only maps local branches of the source to local branches of the target, it maps all refs (including remote-tracking branches, notes etc.) and sets up a refspec configuration such that all these refs are overwritten by a git remote update in the target repository.

使用 --mirror 选项克隆您隐含地使用了 --bare 选项,它创建了一个没有工作树的存储库。
--bare 选项主要用于服务器端或特定用例。

在您的情况下,您不应直接使用镜像仓库,因为它没有工作树,不适合编辑文件或 git 命令,如 git checkout。 你应该 git clone your-mirrored-repo (没有 --bare/--mirror 选项)有一个完整的工作树,从镜像仓库开始,你可以在其中使用所有 git 命令或直接编辑文件。