Git克隆: Just the files,好吗?

标签 git

我想克隆一个 GIT 存储库,而不是以 .git 目录结尾。换句话说,我只想要文件。有没有办法做到这一点?

git clone --no-checkout 与我想要的完全相反(只给我 .git 目录)。

我正在尝试为一个远程 存储库而不是本地存储库执行此操作,这意味着这不是“How to do a “git export” (like “svn export”)”的副本(即使解决方案可能最终是一样的)。

最佳答案

与您要查找的内容最接近的 git 命令是 git archive .
参见 backing up project which uses git : 它将在存档中包含所有文件(如果您使用 git-archive-all 脚本,则包括子模块)

然后您可以在任何地方使用该存档,只返回文件,没有 .git 目录。

git archive --remote=<repository URL> | tar -t

如果您只需要第一层的文件夹和文件:

git archive --remote=<repository URL> | tar -t --exclude="*/*"

仅列出远程仓库的一级文件夹:

git archive --remote=<repository URL> | tar -t --exclude="*/*" | grep "/"

注意:即does not work for GitHub (not supported)

所以你需要 to clone (shallow to quicken the clone step), and then archive locally :

git clone --depth=1 git@github.com:xxx/yyy.git
cd yyy
git archive --format=tar aTag -o aTag.tar

另一种选择是进行浅克隆(如下所述),但将 .git 文件夹放在别处。

git --git-dir=/path/to/another/folder.git clone --depth=1 /url/to/repo

repo 文件夹将只包含文件,不包含 .git

注意:git --git-dircommand git 的一个选项, 不是 git clone .


使用 Git 2.14.X/2.15(2017 年第 4 季度)更新:它将确保避免添加空文件夹

"git archive", especially when used with pathspec, stored an empty directory in its output, even though Git itself never does so.
This has been fixed.

参见 commit 4318094 (2017 年 9 月 12 日)作者 René Scharfe (``) .
推荐人:Jeff King (peff) .
(由 Junio C Hamano -- gitster -- merge 于 commit 62b1cb7 ,2017 年 9 月 25 日)

archive: don't add empty directories to archives

While git doesn't track empty directories, git archive can be tricked into putting some into archives.
While that is supported by the object database, it can't be represented in the index and thus it's unlikely to occur in the wild.

As empty directories are not supported by git, they should also not be written into archives.
If an empty directory is really needed then it can be tracked and archived by placing an empty .gitignore file in it.

关于Git克隆: Just the files,好吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3946538/

相关文章:

git - 如何推送和自动更新我的非裸远程 git 存储库的工作副本?

git - 如何使用 TFS Git 为单独的分支创建单独的工作目录?

git - 为什么“cherry-pick”会导致 git merge 丢弃我的部分提交?

用于从两台机器进行开发的 Git 存储库设置?

git - 我应该如何处理特定于用户的配置文件(例如 : . idea/)?

git: 无法创建上游分支

git clone 致命的 : cannot create work tree dir permission denied

git - 显示git中每个子目录的最新变化

git - 使用 git 编辑对旧提交进行更改的最佳实践

git - 如何将本地 git 项目推送到远程服务器?