Using Git Hook to Clean Jupyter Notebook on Commit


Hiromi Suenaga


February 5, 2022

To avoid merge issues and make PR review easier, we wanted a Git Hook which will: - Clear output of Jupyter Notebook - Only clean the files that were modified for this PR

There are many hook samples in .git/hooks folder. You want to create a file called pre-commit with the following code:

for file in $(git diff --diff-filter=d --cached --name-only | grep -E 'customers/.+\.ipynb$')
    jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace "$file"
    git add "$file"


Run chmod +x pre-commit to make the file executable.

That’s it!

In the next post, we will show you how to create this Git Hook with Makefile.

If the above code gives you the following error:

[NbConvertApp] WARNING | Config option `template_path` not recognized by `NotebookExporter`.

You need to run pip install -U nbconvert to get a newer version (