Forked from guysmoilov/Git pre-commit hook for large files.md
Last active
September 3, 2024 12:50
-
-
Save danmackinlay/6e4a0e5c38a43972a0de2938e6ddadba to your computer and use it in GitHub Desktop.
Revisions
-
danmackinlay revised this gist
Mar 23, 2021 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -43,10 +43,11 @@ To install this for all future repositories curl -L https://gist.github.com/danmackinlay/6e4a0e5c38a43972a0de2938e6ddadba/raw/install.sh | bash ``` For just the current one (erasing any existing pre-commit hooks) ```bash curl -L https://gist.github.com/danmackinlay/6e4a0e5c38a43972a0de2938e6ddadba/raw/pre-commit > .git/hooks/pre-commit chmod a+x .git/hooks/pre-commit ``` ## Credits -
dan revised this gist
Mar 16, 2021 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -37,10 +37,18 @@ You can use in two ways. ## Installation To install this for all future repositories ```bash curl -L https://gist.github.com/danmackinlay/6e4a0e5c38a43972a0de2938e6ddadba/raw/install.sh | bash ``` For just the current one (erasing any existinf pre-commit hooks) ```bash curl -L https://gist.github.com/danmackinlay/6e4a0e5c38a43972a0de2938e6ddadba/raw/pre-commit > .git/hooks/pre-commit ``` ## Credits Based on @kiwidamien's original gist [here](https://gist.github.com/kiwidamien/a6a909ee196be8795b30431079074d64). -
dan revised this gist
Mar 16, 2021 . 1 changed file with 5 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ # Git pre-commit hook for large files This hook warns you before you accidentally commit large files to git. It's very hard to reverse such an accidental commit, so it's better to prevent it in advance. @@ -8,12 +9,14 @@ a script is attached to add this hook to all git repos you create / clone in the Of course, you can just download it directly to the hooks in an existing git repo. ## Configuration The default limit is max 5MB per file. If you feel that your commit is a special case, you can always override the limit with: ``` GIT_FILE_SIZE_LIMIT=42000000 git commit -m "This commit is allowed file sizes up to 42MB" ``` ## Contents * [pre-commit](ddb3329e31b001c1e990e08394a08dc4/raw/pre-commit) : The hook itself * [install.sh](ddb3329e31b001c1e990e08394a08dc4/raw/install.sh) : Installs this hook to your git template directory. @@ -38,16 +41,15 @@ You can use in two ways. curl -L https://gist.github.com/danmackinlay/6e4a0e5c38a43972a0de2938e6ddadba/raw/install.sh | bash ``` ## Credits Based on @kiwidamien's original gist [here](https://gist.github.com/kiwidamien/a6a909ee196be8795b30431079074d64). Adapted from: https://gist.github.com/benmccallum/28e4f216d9d72f5965133e6c43aaff6e [Help from this stackoverflow question](https://stackoverflow.com/questions/2293498/applying-a-git-post-commit-hook-to-all-current-and-future-repos). This version is based on @guysmoilov’s [fork](https://gist.github.com/guysmoilov/ddb3329e31b001c1e990e08394a08dc4) which adds a neat isntaller script. ## Alternatives [pre-commit](https://pre-commit.com/) is "a framework for managing and maintaining multi-language pre-commit hooks" and has a hook you can plug-in called: `check-added-large-files`. pre-commit is built with Python though, so you'll need Python installed. -
dan revised this gist
Mar 16, 2021 . 2 changed files with 31 additions and 31 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,14 +2,11 @@ This hook warns you before you accidentally commit large files to git. It's very hard to reverse such an accidental commit, so it's better to prevent it in advance. Since you will likely want [this script](./raw/pre-commit) to run in all your git repos, a script is attached to add this hook to all git repos you create / clone in the future. Of course, you can just download it directly to the hooks in an existing git repo. ## Configuration The default limit is max 5MB per file. If you feel that your commit is a special case, you can always override the limit with: ``` @@ -20,7 +17,37 @@ GIT_FILE_SIZE_LIMIT=42000000 git commit -m "This commit is allowed file sizes up * [pre-commit](ddb3329e31b001c1e990e08394a08dc4/raw/pre-commit) : The hook itself * [install.sh](ddb3329e31b001c1e990e08394a08dc4/raw/install.sh) : Installs this hook to your git template directory. ## Usage You can use in two ways. 1. Directly as the pre-commit hook in your .git/hooks folder. 2. With [Husky](https://github.com/typicode/husky) by updating your package.json with: ``` "husky": { "hooks": { "pre-commit": "sh ./some-path/pre-commit-prevent-large-files.sh" } } ``` ## Installation ```bash curl -L https://gist.github.com/danmackinlay/6e4a0e5c38a43972a0de2938e6ddadba/raw/install.sh | bash ``` @guysmoilov wrote an installer script over [here](https://gist.github.com/guysmoilov/ddb3329e31b001c1e990e08394a08dc4) that adds this hook to all future repos you clone; how cool! ## Credits Based on @kiwidamien's original gist [here](https://gist.github.com/kiwidamien/a6a909ee196be8795b30431079074d64). Adapted from: https://gist.github.com/benmccallum/28e4f216d9d72f5965133e6c43aaff6e [Help from this stackoverflow question](https://stackoverflow.com/questions/2293498/applying-a-git-post-commit-hook-to-all-current-and-future-repos) ## Alternatives [pre-commit](https://pre-commit.com/) is "a framework for managing and maintaining multi-language pre-commit hooks" and has a hook you can plug-in called: `check-added-large-files`. pre-commit is built with Python though, so you'll need Python installed. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,27 +0,0 @@ -
danmackinlay revised this gist
Feb 15, 2021 . 2 changed files with 42 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ ## Usage You can use in two ways. 1. Directly as the pre-commit hook in your .git/hooks folder. 2. With [Husky](https://github.com/typicode/husky) by updating your package.json with: ``` "husky": { "hooks": { "pre-commit": "sh ./some-path/pre-commit-prevent-large-files.sh" } } ``` ## Installation @guysmoilov wrote an awesome installer script over [here](https://gist.github.com/guysmoilov/ddb3329e31b001c1e990e08394a08dc4) that means this hook will be added to all future repos you clone; how cool! ## Credits Based on @kiwidamien's original gist [here](https://gist.github.com/kiwidamien/a6a909ee196be8795b30431079074d64) ## Alternatives [pre-commit](https://pre-commit.com/) is "a framework for managing and maintaining multi-language pre-commit hooks" and has a hook you can plug-in called: `check-added-large-files`. pre-commit is built with Python though, so you'll need Python installed. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,14 +1,11 @@ #!/bin/bash # This is a pre-commit hook that ensures attempts to commit files that are # larger than $limit to your _local_ repo fail, with a helpful error message. # Maximum file size limit in bytes limit=$(( 5 * 2**20 )) # 5MB limitInMB=$(( $limit / 2**20 )) # Move to the repo root so git files paths make sense repo_root=$( git rev-parse --show-toplevel ) @@ -29,17 +26,21 @@ IFS=' echo "Checking staged file sizes" shouldFail=false # `--diff-filter=d` -> skip deletions for file in $( git diff-index --cached --diff-filter=d --name-only "$against" ); do # Skip for directories (git submodules) if [[ -f "$file" ]]; then file_size=$( ls -lan $file | awk '{ print $5 }' ) if [ "$file_size" -gt "$limit" ]; then echo File $file is $(( $file_size / 2**20 )) MB, which is larger than our configured limit of $limitInMB MB shouldFail=true fi fi done if $shouldFail then echo If you really need to commit this file, you can push with the --no-verify switch, but the file should definitely, definitely be under $limitInMB MB!!! echo Commit aborted exit 1; fi -
benmccallum revised this gist
Feb 1, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ #!/bin/bash # This is a pre-commit hook that ensures attempts to commit files that are # larger than $limit to your _local_ repo fail, with a helpful error message. -
benmccallum revised this gist
Jan 26, 2021 . 1 changed file with 13 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,11 @@ #!/bin/sh # This is a pre-commit hook that ensures attempts to commit files that are # larger than $limit to your _local_ repo fail, with a helpful error message. # Maximum file size limit in bytes limit=$(( 5 * 2**20 )) # 5MB limitInMB=$(( $limit / 2**20 )) # Move to the repo root so git files paths make sense repo_root=$( git rev-parse --show-toplevel ) @@ -26,17 +26,21 @@ IFS=' echo "Checking staged file sizes" shouldFail=false # `--diff-filter=d` -> skip deletions for file in $( git diff-index --cached --diff-filter=d --name-only "$against" ); do # Skip for directories (git submodules) if [[ -f "$file" ]]; then file_size=$( ls -lan $file | awk '{ print $5 }' ) if [ "$file_size" -gt "$limit" ]; then echo File $file is $(( $file_size / 2**20 )) MB, which is larger than our configured limit of $limitInMB MB shouldFail=true fi fi done if $shouldFail then echo If you really need to commit this file, you can push with the --no-verify switch, but the file should definitely, definitely be under $limitInMB MB!!! echo Commit aborted exit 1; fi -
benmccallum revised this gist
Jan 6, 2020 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ ## Usage You can use in two ways. @@ -14,14 +14,14 @@ You can use in two ways. } ``` ## Installation @guysmoilov wrote an awesome installer script over [here](https://gist.github.com/guysmoilov/ddb3329e31b001c1e990e08394a08dc4) that means this hook will be added to all future repos you clone; how cool! ## Credits Based on @kiwidamien's original gist [here](https://gist.github.com/kiwidamien/a6a909ee196be8795b30431079074d64) ## Alternatives [pre-commit](https://pre-commit.com/) is "a framework for managing and maintaining multi-language pre-commit hooks" and has a hook you can plug-in called: `check-added-large-files`. pre-commit is built with Python though, so you'll need Python installed. -
benmccallum revised this gist
Jan 6, 2020 . 1 changed file with 13 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ # Usage You can use in two ways. 1. Directly as the pre-commit hook in your .git/hooks folder. @@ -12,4 +14,14 @@ You can use in two ways. } ``` # Installation @guysmoilov wrote an awesome installer script over [here](https://gist.github.com/guysmoilov/ddb3329e31b001c1e990e08394a08dc4) that means this hook will be added to all future repos you clone; how cool! # Credits Based on @kiwidamien's original gist [here](https://gist.github.com/kiwidamien/a6a909ee196be8795b30431079074d64) # Alternatives [pre-commit](https://pre-commit.com/) is "a framework for managing and maintaining multi-language pre-commit hooks" and has a hook you can plug-in called: `check-added-large-files`. pre-commit is built with Python though, so you'll need Python installed. -
benmccallum revised this gist
Jan 6, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,7 +17,7 @@ if git rev-parse --verify HEAD > /dev/null 2>&1 then against=HEAD else against="$empty_tree" fi # Set split so that for loop below can handle spaces in file names by splitting on line breaks -
guysmoilov revised this gist
Dec 26, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -39,7 +39,7 @@ done if $shouldFail then echo If you really need to commit this file, you can override the size limit by setting the GIT_FILE_SIZE_LIMIT environment variable, e.g. GIT_FILE_SIZE_LIMIT=42000000 for 42MB. Or, commit with the --no-verify switch to skip the check entirely, you naughty boy! echo Commit aborted exit 1; fi -
guysmoilov revised this gist
Dec 26, 2019 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,6 +10,12 @@ Of course, you can just download it directly to the hooks in an existing git rep ## Installation `curl -L https://gist.github.com/guysmoilov/ddb3329e31b001c1e990e08394a08dc4/raw/install.sh | bash` ## Configuration The default limit is max 5MB per file. If you feel that your commit is a special case, you can always override the limit with: ``` GIT_FILE_SIZE_LIMIT=42000000 git commit -m "This commit is allowed file sizes up to 42MB" ``` ## Contents * [pre-commit](ddb3329e31b001c1e990e08394a08dc4/raw/pre-commit) : The hook itself * [install.sh](ddb3329e31b001c1e990e08394a08dc4/raw/install.sh) : Installs this hook to your git template directory. -
guysmoilov revised this gist
Dec 26, 2019 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,8 +3,11 @@ # This is a pre-commit hook that ensures attempts to commit files that are # are larger than $limit to your _local_ repo fail, with a helpful error message. # You can override the default limit of 5MB by supplying the environment variable: # GIT_FILE_SIZE_LIMIT=42000000 git commit -m "This commit is allowed file sizes up to 42MB" # Maximum file size limit in bytes limit=${GIT_FILE_SIZE_LIMIT:-5000000} # Default 5MB limitInMB=$(( $limit / 10**6 )) # Move to the repo root so git files paths make sense -
guysmoilov revised this gist
Dec 26, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ a script is attached to add this hook to all git repos you create / clone in the Of course, you can just download it directly to the hooks in an existing git repo. ## Installation `curl -L https://gist.github.com/guysmoilov/ddb3329e31b001c1e990e08394a08dc4/raw/install.sh | bash` ## Contents * [pre-commit](ddb3329e31b001c1e990e08394a08dc4/raw/pre-commit) : The hook itself -
guysmoilov revised this gist
Dec 26, 2019 . 2 changed files with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ This hook warns you before you accidentally commit large files to git. It's very hard to reverse such an accidental commit, so it's better to prevent it in advance. Since you will likely want [this script](ddb3329e31b001c1e990e08394a08dc4/raw/pre-commit) to run in all your git repos, a script is attached to add this hook to all git repos you create / clone in the future. Of course, you can just download it directly to the hooks in an existing git repo. @@ -11,8 +11,8 @@ Of course, you can just download it directly to the hooks in an existing git rep `curl -L https://gist.github.com/guysmoilov/eba4fcdb7eb148f32b9b5bc165d6e933/raw/install.sh | bash` ## Contents * [pre-commit](ddb3329e31b001c1e990e08394a08dc4/raw/pre-commit) : The hook itself * [install.sh](ddb3329e31b001c1e990e08394a08dc4/raw/install.sh) : Installs this hook to your git template directory. ## Credits Adapted from: https://gist.github.com/benmccallum/28e4f216d9d72f5965133e6c43aaff6e This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -24,7 +24,7 @@ if [ -f "$PRECOMMIT_HOOK" ]; then fi echo "Downloading the hook to $PRECOMMIT_HOOK" curl -L https://gist.github.com/guysmoilov/ddb3329e31b001c1e990e08394a08dc4/raw/pre-commit -o "$PRECOMMIT_HOOK" 2> /dev/null echo "Making it executable" chmod +x "$PRECOMMIT_HOOK" -
guysmoilov created this gist
Dec 26, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ # Git pre-commit hook for large files This hook warns you before you accidentally commit large files to git. It's very hard to reverse such an accidental commit, so it's better to prevent it in advance. Since you will likely want [this script](eba4fcdb7eb148f32b9b5bc165d6e933/raw/pre-commit) to run in all your git repos, a script is attached to add this hook to all git repos you create / clone in the future. Of course, you can just download it directly to the hooks in an existing git repo. ## Installation `curl -L https://gist.github.com/guysmoilov/eba4fcdb7eb148f32b9b5bc165d6e933/raw/install.sh | bash` ## Contents * [pre-commit](eba4fcdb7eb148f32b9b5bc165d6e933/raw/pre-commit) : The hook itself * [install.sh](eba4fcdb7eb148f32b9b5bc165d6e933/raw/install.sh) : Installs this hook to your git template directory. ## Credits Adapted from: https://gist.github.com/benmccallum/28e4f216d9d72f5965133e6c43aaff6e [Help from this stackoverflow question](https://stackoverflow.com/questions/2293498/applying-a-git-post-commit-hook-to-all-current-and-future-repos) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ #!/bin/sh set -e echo "Starting install script..." SET_GIT_TEMPLATE_DIR=false EXISTING_TEMPLATE=$(git config --global init.templateDir || echo "") if [ -z "$EXISTING_TEMPLATE" ]; then echo "Creating a new global git template dir at ~/.git_template" mkdir ~/.git_template EXISTING_TEMPLATE="$(cd ~; pwd -P)/.git_template" SET_GIT_TEMPLATE_DIR=true else EXISTING_TEMPLATE="$(eval cd $(dirname "$EXISTING_TEMPLATE"); pwd -P)/$(basename "$EXISTING_TEMPLATE")" echo "Using existing git template dir: $EXISTING_TEMPLATE" fi HOOKS_DIR="$EXISTING_TEMPLATE/hooks" PRECOMMIT_HOOK="$HOOKS_DIR/pre-commit" echo "Creating hooks dir if it doesn't already exist: $HOOKS_DIR" mkdir -p "$HOOKS_DIR" if [ -f "$PRECOMMIT_HOOK" ]; then echo "Cannot install hook as it's already defined: '$PRECOMMIT_HOOK'" >&2 exit 1 fi echo "Downloading the hook to $PRECOMMIT_HOOK" curl -L https://gist.github.com/guysmoilov/eba4fcdb7eb148f32b9b5bc165d6e933/raw/pre-commit -o "$PRECOMMIT_HOOK" 2> /dev/null echo "Making it executable" chmod +x "$PRECOMMIT_HOOK" if [ "$SET_GIT_TEMPLATE_DIR" = true ]; then echo "Defining ~/.git_template as the global git template dir" git config --global init.templateDir '~/.git_template' fi echo -e "\nDone! Any future git repo created in this user profile will contain the hook\n" This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ #!/bin/sh # This is a pre-commit hook that ensures attempts to commit files that are # are larger than $limit to your _local_ repo fail, with a helpful error message. # Maximum file size limit in bytes limit=5000000 # 5MB limitInMB=$(( $limit / 10**6 )) # Move to the repo root so git files paths make sense repo_root=$( git rev-parse --show-toplevel ) cd $repo_root empty_tree=$( git hash-object -t tree /dev/null ) if git rev-parse --verify HEAD > /dev/null 2>&1 then against=HEAD else against="$empty_tree" fi # Set split so that for loop below can handle spaces in file names by splitting on line breaks IFS=' ' echo "Checking staged file sizes" shouldFail=false for file in $( git diff-index --cached --name-only $against ); do file_size=$( ls -la $file | awk '{ print $5 }') if [ "$file_size" -gt "$limit" ]; then echo File $file is $(( $file_size / 10**6 )) MB, which is larger than our configured limit of $limitInMB MB shouldFail=true fi done if $shouldFail then echo If you really need to commit this file, you can push with the --no-verify switch, but the file should definitely, definitely be under 10MB!!! echo Commit aborted exit 1; fi -
benmccallum created this gist
Nov 28, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ You can use in two ways. 1. Directly as the pre-commit hook in your .git/hooks folder. 2. With [Husky](https://github.com/typicode/husky) by updating your package.json with: ``` "husky": { "hooks": { "pre-commit": "sh ./some-path/pre-commit-prevent-large-files.sh" } } ``` Based on @kiwidamien's original gist [here](https://gist.github.com/kiwidamien/a6a909ee196be8795b30431079074d64) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ #!/bin/sh # This is a pre-commit hook that ensures attempts to commit files that are # are larger than $limit to your _local_ repo fail, with a helpful error message. # Maximum file size limit in bytes limit=5000000 # 5MB limitInMB=$(( $limit / 10**6 )) # Move to the repo root so git files paths make sense repo_root=$( git rev-parse --show-toplevel ) cd $repo_root empty_tree=$( git hash-object -t tree /dev/null ) if git rev-parse --verify HEAD > /dev/null 2>&1 then against=HEAD else against=empty_tree fi # Set split so that for loop below can handle spaces in file names by splitting on line breaks IFS=' ' echo "Checking staged file sizes" shouldFail=false for file in $( git diff-index --cached --name-only $against ); do file_size=$( ls -la $file | awk '{ print $5 }') if [ "$file_size" -gt "$limit" ]; then echo File $file is $(( $file_size / 10**6 )) MB, which is larger than our configured limit of $limitInMB MB shouldFail=true fi done if $shouldFail then echo If you really need to commit this file, you can push with the --no-verify switch, but the file should definitely, definitely be under 10MB!!! echo Commit aborted exit 1; fi