Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 122 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save 33eyes/431e3d432f73371509d176d0dfb95b6e to your computer and use it in GitHub Desktop.
Save 33eyes/431e3d432f73371509d176d0dfb95b6e to your computer and use it in GitHub Desktop.
How to commit jupyter notebooks without output to git while keeping the notebooks outputs intact locally
  1. 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'  
  1. Create a .gitattributes file inside the directory with the notebooks

  2. 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.

@konradmb
Copy link

konradmb commented Aug 16, 2023

the command you ran will not work

@33eyes It will work, but won't output anything, just freeze. So that means jupyter command is available in PATH. But if running it from git bash gives 'unrecognized command', that means it's not in PATH anymore, so I suspect it's not in global PATH env var and that's how git would run it either when from git bash, cmd.exe, windows terminal or vscode etc. etc.

@air-kyi
Copy link

air-kyi commented Aug 30, 2023

hi everyone, I solved the issue. it's because I was in a virtual environment and needed to add that specific installation of python to PATH.

  1. in Anaconda prompt, type where jupyter or just figure out where your jupyter.exe is stored in your venv
(orf) C:\Users\kyi>where jupyter
C:\Users\kyi\AppData\Local\anaconda3\envs\orf\Scripts\jupyter.exe
  1. add this path (without the jupyter.exe part) to a new variable in PATH

@NickCrews
Copy link

NickCrews commented Sep 26, 2023

@33eyes can you add the 4th step of

  1. Run git add --renormalize . to go through all of your existing notebook files and scrub the outputs. Otherwise, you could get heinous merge conflicts later.

PS to people having PATH issues: If you are using VScode, perhaps your issue is caused by vscode running all of the git commands in a weird environment typically different from what you have on the command line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment