Skip to content

Instantly share code, notes, and snippets.

@kunicmarko20
Last active February 15, 2023 08:30
Show Gist options
  • Save kunicmarko20/4ac45ae65d21eb524a7fc441f885712f to your computer and use it in GitHub Desktop.
Save kunicmarko20/4ac45ae65d21eb524a7fc441f885712f to your computer and use it in GitHub Desktop.
Pre Commit Git hook that will remove file from commit if it contains "// warn-before-commit" string
#!/bin/bash
top_level_path="$(git rev-parse --show-toplevel)";
while read file_name; do
if grep -q "// warn-before-commit" "$top_level_path/$file_name"; then
git reset $file_name;
echo "File $file_name, was not commited because 'warn-before-commit' string was found."
fi
done < <(git diff --cached --name-only --diff-filter=ACM)
@cryptiklemur
Copy link

Does this actually work for you? The files I reset still somehow get added to my commit. If I add --soft or git restore --staged it completely deletes the changes.

@kunicmarko20
Copy link
Author

Does this actually work for you? The files I reset still somehow get added to my commit. If I add --soft or git restore --staged it completely deletes the changes.

Hey, it used to work without a problem when I created it, but haven't used this in 2y for sure, so not sure if something changed around hooks potentially

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment