Skip to content

Instantly share code, notes, and snippets.

@Pezmc
Created August 7, 2014 18:38
Show Gist options
  • Save Pezmc/0b99040930b5e2a009f8 to your computer and use it in GitHub Desktop.
Save Pezmc/0b99040930b5e2a009f8 to your computer and use it in GitHub Desktop.
Git pre-commit prevent commit of certain files
#!/bin/sh
error_found=false
declare -a BANNED_PATTERNS=("app/config/database.php")
# Terminal
if [ -t 1 ]; then
ncolors=$(tput colors)
if test -n "$ncolors" && test $ncolors -ge 8; then
normal="$(tput sgr0)"
red="$(tput setaf 1)"
fi
fi
# Check banned files
for FILE in `git diff --cached --name-only` ; do
for PATTERN in $BANNED_PATTERNS; do
if [[ $FILE == $PATTERN ]]
then
echo "Banned file ${red}$FILE${normal} cannot be committed"
error_found=true
fi
done
done
# Report errors
if [ "$error_found" = true ]
then
echo "To commit anyway, use --no-verify"
exit 1
fi
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment