git clone,忽略文件

7
我想克隆一个GitHub仓库,其中包含一个巨大的.exe文件。(为什么呢?!)我对.exe文件完全没有用处,而且它的大小比其他所有文件加在一起还要大得多。有办法在克隆时忽略这个文件吗?我的猜测是我最好向作者请求创建一个不包含exe的分支!希望能有一些好的方法解决这个问题。

相关链接:https://dev59.com/u2Yq5IYBdhLWcg3woCC7 - Lajos Veres
我肯定是先看到了这个问题,但并不满意。所以,我问了一个稍微更具体的问题。有一个答案指出 git clone 总是会克隆整个仓库,因此我想知道是否有任何方法可以实现我的目标,即不必下载 exe 文件,但又可以使用 git 命令自动化此过程。 - mrKelley
1
下面所接受的答案非常有效。 - mrKelley
如果您查看链接答案中的评论(或存储库的内部),您将看到大型exe文件已下载,但它已从您的工作副本中过滤掉了。克隆操作在下载时不关心稀疏检出属性,该属性仅在从.git文件夹复制文件到工作副本时才重要。 - Lajos Veres
我该如何验证“存储库内部”的内容?我应该在哪里查找那些我认为已经忽略的.exe文件?我在存储库中没有看到它们,但它们是否被缓存到某个地方了? - mrKelley
显示剩余3条评论
2个回答

7

2020+:使用更近期的{{link1:git sparse-checkout命令}}(我在这里介绍):

这将实际上是克隆而不下载任何东西:

git clone --filter=blob:none --no-checkout https://github.com/git/git
cd git

我在这里详细说明排除

# Disablecone mode in .git/config.worktree
git config core.sparseCheckoutCone false

# remove .git\info\sparse-checkout
git sparse-checkout disable

# Add the expected pattern, to include just a subfolder without top files:
git sparse-checkout set !/your.exe

# populate working-tree with only the right files:
git read-tree -mu HEAD

2014年:您可以尝试使用稀疏检出,具体步骤如下:

  • 初始化一个空仓库,
  • 添加指向GitHub仓库的远程地址:git remote add -f origin <url>
  • git config core.sparsecheckout true
  • .git/info/sparse-checkout文件中指定您想要加载的内容。

根据您的情况:

/*
!yourExe

你现在可以做一个:
git pull origin master

那应该下载所有东西,除了你的exe文件。

这个下载所有东西,包括exe文件。只是最后不检查它。它仍然会占用你的.git空间。 - Atemu
@Atemu 你说得对:这个2014年的答案并不令人满意。我已经更新了它,使用了2020年更近期的命令 git sparse-checkout。如果这样做效果更好,请告诉我。 - VonC

1
只是为了对VonC的回答进行一些小的调整,以下是我的步骤:
# Clone without downloading files
git clone --no-checkout --filter=blob:none https://.../<your_git_repo>.git

# Go inside cloned repo
cd <your_git_repo>

# Disable cone mode in .git/config.worktree
git config core.sparseCheckoutCone false

# Remove .git\info\sparse-checkout
git sparse-checkout disable

# Set retrieval to come: all blobs...
git sparse-checkout set '/*'

# ... except this file
git sparse-checkout add '!/<path_to_file_inside_Git_repo>'

# Now download the list of files just set
git read-tree -mu HEAD

如果你和我一样使用的是Unix系统,那么在使用单引号时要非常准确:这是我在回答中无法实现的主要原因,除了sparse-checkout的add/set组合之外。

不错的逐步过程。点赞。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接