3

GitHub documentation on large files states:

If you attempt to add or update a file that is larger than 50 MB, you will receive a warning from Git.

GitHub blocks pushes that exceed 100 MB.

I want to configure it in a way that it blocks any pushes with files larger than some predefined threshold (e.g. 100kb).

Is this possible? How?

Maybe via the branch protection rules? I saw some examples that the branch protection rules can include custom status checks. The status checks, these are GitHub actions? So I could maybe add a custom GitHub CI action which checks for a valid commit (valid = any changed file is below the size limit).

Maybe it's not really possible yet. I also asked in the official community forum.

(Note: If possible, I would prefer a pure-Git answer, independent of GitHub. With pure Git, this is possible via a server-side pre-receive hook. See here. However, on GitHub, I cannot have a custom pre-receive hook, so this is not an option.)

Albert
  • 65,406
  • 61
  • 242
  • 386
  • Given that you want this to happen *on GitHub*, this becomes a GitHub-only issue, not a Git programming question. I believe the answer to the question as asked is simply "you can't do that" but you should ask the GitHub folks themselves. It might be a selling-feature for them if they could make this configurable (perhaps letting free users turn it down and paid users turn it up depending on mow much they pay ). – torek Jun 12 '22 at 00:16
  • You could, in a pre-push hook, examine each of the commits you're proposing to send to see if any of them contain any files larger than some size. But a pre-push hook is by definition specific to the *client* repository in which one is running `git push` and can always be defeated. – torek Jun 12 '22 at 00:18
  • @torek If there is a pure Git solution (independent of GitHub), this would be even better. I'm not sure what's possible via Git hooks. (That's why I added the 'git' tag back.) Such hook needs to run on the server-side of course. Not sure if there is any Git mechanism for that. – Albert Jun 12 '22 at 00:18
  • You cannot have your client Git run a server thing: that would be insecure. (Well, you can run anything they've provided, via some unspecified mechanism such as "branch names matching given patterns" or some such - but we're back to "GitHub must provide this for you".) – torek Jun 12 '22 at 00:20
  • @torek What about the server-side pre-receive hook? https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks – Albert Jun 12 '22 at 00:22
  • 1
    GitHub have their own and it contains the size check (and protected-branch checks and so on). They won't let you run arbitrary code in their pre-receive hook. But the size check is pretty clearly hardcoded at the moment. – torek Jun 12 '22 at 00:24
  • I see. Thanks. Actually, I just found that they allowed to have a custom pre-receive hook at some earlier point in time. https://stackoverflow.com/questions/10864903/does-github-allow-pre-receive-hooks – Albert Jun 12 '22 at 00:26
  • That's about "GitHub Enterprise", which is a thing you buy from them to run on your own server. It's then your machine, so you get to play with the pre-receive hooks. – torek Jun 12 '22 at 00:29

2 Answers2

0

Depends on what are you trying to do. You could always use .gitignore

Here can you find some examples https://github.com/github/gitignore

You create a file called .gitignore, in the file you add a folder name node_modules, and GitHub won't include it. Or let's say all txt files would be .txt in the .gitignore file.

doley
  • 35
  • 4
  • 2
    I want to block all attempts to push files which are larger than some predefined limit. Also, this check should be done server-side on GitHub, not client-side. – Albert Jun 12 '22 at 00:12
  • Check this one out -> https://stackoverflow.com/questions/17382375/github-file-size-limit-changed-6-18-13-cant-push-now , what is your error ? – doley Jun 12 '22 at 00:20
  • 1
    See my question. I want to configure this limit. – Albert Jun 12 '22 at 00:22
  • https://stackoverflow.com/questions/39576257/how-to-limit-file-size-on-commit – doley Jun 12 '22 at 00:25
  • Actually more related is [this](https://stackoverflow.com/questions/7147699/limiting-file-size-in-git-repository). However, it seems that I cannot edit a custom pre-receive hook on GitHub. And any client-side hooks are also no real solutions. – Albert Jun 12 '22 at 00:28
  • 1
    @Albert: [this answer](https://stackoverflow.com/a/57977351/1256452) there is the right one; it can be adapted for use in a pre-push hook, but as we have both noted already, that has its own flaws. Short of running your own server or getting a new GitHub feature, though, it's probably the best you'll get. – torek Jun 12 '22 at 00:33
0

Most of the time I try to commit a large file, it's an error, so I use a pre-commit hook. If you don't have any hooks setup, you can just create the file /.git/hooks/pre-commit and put the following bash in it

#!/bin/bash
max_file_size=10000000
for filename in $(git diff --cached --name-only --diff-filter=A HEAD); do
    filesize=$(stat -c%s "$filename")
    if  (( filesize > max_file_size )) ; then
        echo "The file "$filename" is more than $max_file_size bytes. Git commit cancled." >/dev/stderr
        exit -1
    fi
done

Then make the hook executable with chmod +x /.git/hooks/pre-commit. This script will block files with more than 10000000 bytes. Some people get fancy with their pre-commits using them to run all sorts of tests and code quality checks, using tools like the pre-commit framework.

Hope this helps!

yberman
  • 306
  • 1
  • 11