I strongly recommend putting the following little script in .git/hooks/pre-commit
. It uses nbconvert
on all .ipynb
files that are staged to be committed, and if after stripping all the output there's no changes to be committed it exits. That last part is important because otherwise you'll be making useless empty commits. Since it only runs on notebooks you have committed it won't remove all the output from other notebooks you're still working on.
#!/bin/bash
for f in $(git diff --name-only --cached); do
if [[ $f == *.ipynb ]]; then
jupyter nbconvert --clear-output --inplace $f
git add $f
fi
done
if git diff --name-only --cached --exit-code
then
echo "No changes detected after removing notebook output"
exit 1
fi
That script plus the appropriate .gitignore
entries should ensure that your Git history is kept clear from unwanted Jupyter output.
Here's a Husky compatible variant, just save it in .husky/pre-commit
.
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
for f in $(git diff --name-only --cached); do
case "$f" in
*".ipynb") jupyter nbconvert --clear-output --inplace $f && git add $f ;;
esac
done
if git diff --name-only --cached --exit-code
then
echo "No changes detected after removing notebook output"
exit 1
fi