Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从git respository中排除大文件_Git_Powershell_Gitignore - Fatal编程技术网

从git respository中排除大文件

从git respository中排除大文件,git,powershell,gitignore,Git,Powershell,Gitignore,我想告诉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" &

我想告诉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中运行代码时,它会说

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将始终排除大于250MB的文件

git add .
git commit -m "Initial commit"
git push -u origin main


我如何才能正确地告诉git不要将大于250MB的文件提交到我的远程存储库?

您的答案的问题是,您只忽略在添加到
时发现的那些文件。gitignore
。如果要在每次提交时进行检查,您有2个选项:

  • 您可以使用git别名仅添加小于250MB的文件,方法与您链接的文章中的说明相同。问题是每次提交时都必须记住使用它,而且您必须将所有(有效)文件添加到索引中,而您可能希望将工作目录更改拆分为2次提交

  • 使用
    预提交
    钩子检查索引中每个文件的大小是否小于250MB。我在Linux上,所以我无法为您编写脚本(实际上我不确定它是否在Git bash for Windows上工作),但您主要需要2个命令:

    • git ls files-s
      :获取索引中每个文件的名称和blob
    • git cat file-s
      :获取从上一个命令检索到的
      的大小
    通过这种方式,如果任何文件大于最大大小,则可以终止提交过程。当这个钩子失败时,您可以运行PS脚本来忽略更大的文件


  • 此注释将创建一个
    .gitignore
    文件,其中包含注释
    #超过250 MB的文件
    写入输出“#超过250 MB的文件”|输出文件-文件路径。\.gitignore
    。此命令将添加超过一定大小的所有文件,但文件路径在引号之间给出,而不是作为相对文件路径:
    forfiles/P“C:\Users\UserName\Folder”/M*/S/C“CMD/C if@fsize gtr 250000000 echo@path”| Out file-FilePath-Append.\.gitignore