- 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 is useful if you sometimes want to add specific notebooks with their cell outputs intact to git, while still having the default behavior of clearing out cells.
- When adding to git a notebook whose cell outputs you want to keep, instead of the usual
git add <path to your notebook>
command, use this:git -c filter.strip-notebook-output.clean= add <path to your notebook>
I am having trouble getting the filter to actually activate. Does anyone know what to do in this case? I can only get it working on one device, but not the same repository on a different device, for two different repositories. I am adding the .gitattributes and .git/config on both different devices.
Working on: Python 3.11.4, conda 23.5.2, git 23.9.1.windows.1
Not working on: Python 3.10.11, conda 23.3.1, git 2.41.0.windows.3
Another source says I have to do git add --renormalize:
But this didn't work for me, git bash didn't like the jupyter command ('unrecognized command') since git and jupyter are not in the same environment.