- Add a filter to git config by running the following command in bash inside the repo:
git config filter.strip-notebook-output.clean 'jupyter nbconvert --ClearOutputPreprocessor.enabled=True --to=notebook --stdin --stdout --log-level=ERROR'
-
Create a
.gitattributes
file inside the directory with the notebooks -
Add the following to that file:
*.ipynb filter=strip-notebook-output
After that, commit to git as usual. The notebook output will be stripped out in git commits, but it will remain unchanged locally.
This gist is based on @dirkjot's answer to this StackOverflow question.
Wow, this worked a treat!
I tend to run Jupyter in a Docker container. So, if you want to run the Notebook output cleaning preprocessor using a Jupyter container instead, all you need to do is tweak the git config like this:
I've set it as a global config parameter, so that it applies to all repos.