Skip to content

Instantly share code, notes, and snippets.

@MrSunshyne
Created October 2, 2023 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrSunshyne/023a404c4946636570725080bccf170c to your computer and use it in GitHub Desktop.
Save MrSunshyne/023a404c4946636570725080bccf170c to your computer and use it in GitHub Desktop.
Use a global pre-commit to prevent committing tracked files
mkdir ~/global-git-hooks
code ~/global-git-hooks/pre-commit
chmod +x ~/global-git-hooks/pre-commit
git config --global core.hooksPath ~/global-git-hooks
#!/bin/bash
echo "Running pre-commit checks..."
# Define the directory to check
DIRECTORY_TO_CHECK="/home/sun/Projects/<AND_SO_ON>"
# Define a list of files that should not be committed in the "entropy" directory
FILES_TO_IGNORE=("file1.text" "file2.txt")
# Check if any of the ignored files in the specified directory are about to be committed
for file in "${FILES_TO_IGNORE[@]}"; do
if git diff --cached --name-only | grep -q "$file"; then
echo "WARNING: File '$file' is about to be committed but should be ignored."
echo "Please unstage or remove it before committing."
exit 1
fi
done
# If no ignored files are found in the specified directory, continue with the commit
echo "Files not found in commit. Continuing..."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment