这是针对此问题的 hack 解决方案。您所指的原始 git 过滤器已被形式化为包nbstripout
( pip3 install nbstripout
),但您可以将任何过滤器放入此脚本中,它的工作方式相同。我假设您想为用户而不是特定的 repo 配置它。
在~/.gitconfig
中,添加一个名为 的新差异驱动程序git-nb-clean-diff
:
[diff "git-nb-clean-diff"]
command = git-nb-clean-diff
在~/.config/git/attributes
中,将笔记本配置为与该差异驱动程序进行差异:
*.ipynb diff=git-nb-clean-diff
现在我们需要制作实际的差异驱动程序!在~/bin/git-nb-clean-diff
(必须有这个文件名,但位置是可选的):
#!/bin/bash
# pass the stripped working tree file and the repo copy
# to meld for diffing
meld <(cat $1 | nbstripout) $2
最后,我们使这个文件可执行
chmod +x ~/bin/git-nb-clean-diff
并将其添加到路径中,以便 git 在运行时可以找到我们的差异驱动程序
echo "PATH=$PATH:~/bin" >> ~/.bashrc
# reload the edited .bashrc
source ~/.bashrc