Contents

Removing Exif Data From Images in Git

Cleaning History

In WSL, strip everything:

1
2
3
python3 ./strip_exif.py "$(find content/posts/static -path ./themes -prune -o -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.gif" \) -print)"
git commit -m "Something"
git push origin main

Then, install BFG, and run it to remove all of the images from our previous commits (except the most recent one, where we’ve stripped everything of EXIF information).

1
2
sudo apt install default-jre
java -jar bfg-1.14.0.jar -D '*.{png,jpg,PNG,JPEG,JPG,jpeg}' notes.git

Then follow the rest of the instructions on the BFG webpage:

1
2
3
cd some-big-repo.git
git reflog expire --expire=now --all && git gc --prune=now --aggressive
git push

Guarding The Future

All we need to do is create an executable pre-commit file, which can be created by copying the existing pre-commit.sample file within .git/hooks. See here for more details.

The pre-commit file seems to execute in a self-contained copy of Bash, per this SO answer, which means we can use our favorite Unix utilities. Following the guidance from here, my pre-commit file just finds the changed files, and passes them to the strip_exif.py python script. Here’s the most basic version - there’s more to be found in the actual pre-commit file.

1
2
3
#!/bin/sh
python ./strip_exif.py "$(git diff --cached --name-only --diff-filter=ACMR | grep -iE '\.(png|jpe?g|gif)$')"
exit $?

Note that the script seems to always execute from the root of the git repo, which means we can hard-code paths to our python scripts and such. On my Windows install, it seems to be executing C:\Program Files\Python310\python.exe, which I’d guess is based on the system PATH. Presumably, the same would be true on Linux.

Also - TIL that the .git folder isn’t actually committed to the repo! This means all of your git hooks are local. I’ve moved the strip_exif.py script and the pre-commit script into the actual Git repo (strip_exif and pre-commit), and then added a symlink from inside the .git/hooks folder with ln -s ../../content/posts/static/pre-commit ..