+++ title = "Cleaning exif data with git pre-commit" date = 2025-02-15T19:57:01 slug = "remove-exif-git-hook" description = "took longer then it probably should have 😊"
[taxonomies] tags = ["mildrant", "tutorial"] +++
I saw this post from jade.fyi on using a git hook to clear exif data from your images before you commit them and realized I should probably implement that too lol. Interestingly jade also uses zola for her site but she used pre-commit hooks whereas I wanted to do something that used native git hooks.
I started with the naive method of just having a .git/hooks/pre-commit file that would run exiftool on the input but after realizing that hooks placed there wouldn't be synced to the repo decided that wasn't the best way. I moved to using a script that would symlink files from the hooks directory to .git/hooks. It worked moderately well but due to the fact that I used (yes I feel the shame admitting this :uw_embarrassed:) #!/bin/bash instead of #!/usr/bin/env bash. Not realizing my mistake and believing it to be related to the symlink I found this stack overflow answer which taught me that you can use git config core.hooksPath hooks to move the hooks directory to ./hooks in the root of your repo! After doing that and it still not working (i feel very dense writing this lol) I finally realized that the shebang was wrong and then it worked!
{{ img(id="https://hc-cdn.hel1.your-objectstorage.com/s/v3/f2ba3f2dbad8c67eccc42ddbb51bc7128f85d049_9049d20038cc3058acee1bbe58c5ac3f.png" alt="the commit hook finally working!" caption="phew") }}
Is there anything at all to learn from this? Well yes actually! You can use the script below and the git config core.hooksPath hooks setting to scrub your own images!
hooks/pre-commit
#!/usr/bin/env bash
# Check if exiftool is installed
if ! command -v exiftool &> /dev/null; then
echo "Error: exiftool is not installed. Please install it." >&2
exit 1
fi
while read -r file; do
case "$file" in
*.jpg|*.jpeg|*.png|*.gif|*.tiff|*.bmp)
echo "Removing EXIF data from: $file" >&2
exiftool -all= --icc_profile:all -tagsfromfile @ -orientation -overwrite_original "$file"
if [ $? -ne 0 ]; then
echo "Error: exiftool failed to process $file" >&2
exit 1
fi
git add "$file"
;;
*)
;;
esac
done < <(git diff --cached --name-only --diff-filter=ACMR)
exit -0
if you want to add something or comment on the post then I posted about it on bluesky: https://bsky.app/profile/dunkirk.sh/post/3liaybkkas226