git - 从 git 存储库中排除大文件
问题描述
我想告诉 git 不要将大于 250 MB 的文件提交到我的远程存储库。
首先,启动存储库:
cd existing_folder
git init
git remote add origin https://git.xxx.com/username/repository_name.git
接下来,我想告诉 git,不要将大于 200 MB 的文件提交到我的远程存储库。为此,我在 Stackoverflow 上遇到了这个答案,它建议运行以下代码。
echo "# Files over 250mb" >> .gitignore
find * -size +250M -type f -print >> .gitignore
但是,当我在 Windows PowerShell ISE 中运行代码时,它说
PS C:\Users\Username\Rep> find * -size +250M -type f -print >> .gitignore
find : FIND: Parameter format not correct
At line:1 char:1
+ find * -size +250M -type f -print >> .gitignore
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (FIND: Parameter format not correct:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
接下来,我将只运行以下代码。据我了解,理论上,从现在开始,git 总是会排除大于 250 MB 的文件。
git add .
git commit -m "Initial commit"
git push -u origin main
如何正确告诉 git 不要将大于 250MB 的文件提交到我的远程存储库?
解决方案
您的答案的问题是您只忽略在附加到.gitignore
. 如果您想在每次提交时都进行检查,您有两种选择:
您可以使用 git 别名仅添加小于 250MB 的文件,方法与您链接的帖子的接受答案中解释的方式相同。这样做的问题是您必须记住每次提交时都使用它,而且您被迫将所有(有效)文件添加到索引中,而不是您可能希望将工作目录更改拆分为 2 个提交。
使用
pre-commit
钩子检查索引中每个文件的大小是否小于 250MB。我在 Linux 上,所以我无法为您编写脚本(实际上我不确定它是否适用于 Windows 的 Git bash),但您主要需要 2 个命令:git ls-files -s
: 你得到索引中每个文件的名称和 blob。git cat-file -s <blob-id>
: 你得到<blob-id>
, 从上一个命令中检索到的大小。
通过这种方式,如果任何文件大于最大大小,您可以终止提交过程。当这个钩子失败时,你可以运行 PS 脚本来忽略更大的文件。
推荐阅读
- python - 我可以使数组中的 int 值具有属性吗?
- r - R double for 循环遍历列表
- reactjs - React 中什么是无用的构造函数?
- julia - 如何将 Juia 便携式或独立版从 1.5.0 更新到 1.5.1
- html - 活动按钮在每个分区中始终处于活动状态
- android - 使用外部订阅付款的 Android 应用内计费
- javascript - 使用 WKWebView Objective C 在 AngularJS 中表单发布
- c - 如何像malloc一样使用mmap?
- json - 如何为主表和详细表发送 POST?
- django - 通用 UpdateView 不适用于 slug