1

I would like to tell git not to commit files larger than 250 MB to my remote repository.

First, initiated the repository:

cd existing_folder
git init
git remote add origin https://git.xxx.com/username/repository_name.git

Next, I would like to tell git, not to commit files larger than 200 MB to my remote repository. For this, I came across this answer on Stackoverflow which suggests to run the following code.

echo "# Files over 250mb" >> .gitignore
find * -size +250M -type f -print >> .gitignore

However, when I run the code in Windows PowerShell ISE it says

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

Next I would have just run the following code. As I understand, in theory, from now on git would always exclude files larger than 250 MB.

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

How can I correctly tell git not to commit files larger than 250MB to my remote repository?

Stücke
  • 868
  • 3
  • 14
  • 41
  • This comment will create a `.gitignore` file with the comment `# Files over 250 MB`: `Write-Output "# Files over 250 MB" | Out-File -FilePath .\.gitignore`. And this command will add all files above a certain size but the file path is given in between quotation marks and not as a relative file path: `forfiles /P "C:\Users\UserName\Folder" /M *.* /S /C "CMD /C if @fsize gtr 250000000 echo @PATH" | Out-File -FilePath -Append .\.gitignore`. – Stücke Feb 22 '21 at 08:06

1 Answers1

1

The problem with your answer is that you ignore only those files found in the moment you append to the .gitignore. If you want to do the check every time you commit, you have 2 options:

  1. You can use a git alias to add only files smaller than 250MB, in the same way explained in the accepted answer of the post you linked. The problem with this is that you have to remember to use it every time you commit, moreover you are forced to add all the (valid) files to the index, instead you could want to split the working directory changes into 2 commits.

  2. Use a pre-commit hook to check if the size of each file in the index is less than 250MB. I am on Linux, so I cannot write the script for you (actually I am not sure if it works on Git bash for Windows), but you mainly need 2 commands:

    • git ls-files -s: you get the name and blobs of every file in the index.
    • git cat-file -s <blob-id>: you get the size of <blob-id>, retrieved from the previous command.

    In this way you can terminate the commit process if any file is bigger than the maximum size. When this hook fails, then you can run the PS script to ignore bigger files.

Marco Luzzara
  • 5,540
  • 3
  • 16
  • 42