Giter Club home page Giter Club logo

Comments (11)

craffel avatar craffel commented on July 23, 2024 2

Here's how I accomplished it:

git filter-branch --tree-filter 'if [ -f foo.ipynb ]; then nbstripout foo.ipynb; fi'

The if syntax is because if foo.ipynb was not checked in from the first commit, then nbstripout will fail and the filter-branch operation will abort. Another way to avoid that would be to add an --ignore-unmatch flag to nbstripout, like git rm has, which would be pretty straightforward.

This script is great, by the way. Now, if only there was a way to get git diff to ignore cell output too...

from nbstripout.

belteshassar avatar belteshassar commented on July 23, 2024 1

Thanks for this. It worked great. Any reason it's not been added to the README yet? Here's an update that uses --index-filter and operates on all ipynb-files in the repo:

git filter-branch -f --index-filter '
    git checkout -- :*.ipynb
    find . -name '*.ipynb' -exec nbstripout {} +
    git add . --ignore-removal
'

If the repo is large and the notebooks are in a subdirectory it will run faster with git checkout -- :<subdir>/*.ipynb. You'll get a warning from git for commits where there is no notebook. If your really annoyed by this you can pipe stderr to /dev/null.

Naturally, if your repo contains mainly notebooks, you might as well use --tree-filter:

git filter-branch -f --tree-filter 'find . -name '*.ipynb' -exec nbstripout {} +'

from nbstripout.

kynan avatar kynan commented on July 23, 2024

Yes, you can do this already by calling the nbstripout binary in a git filter-branch pipeline. I don't have a command line handy, but if you're happy to craft one, I'd be happy to add it to the README!

from nbstripout.

kynan avatar kynan commented on July 23, 2024

Thanks, that was about the command I would have imagined. Have you tried with --index-filter too?

Note sure what you mean about diff? If you install the nbstripout filter it will also be used by git diff...

from nbstripout.

craffel avatar craffel commented on July 23, 2024

Thanks, that was about the command I would have imagined. Have you tried with --index-filter too?

No, I only tried --tree-filter, is there an advantage to one or the other?

If you install the nbstripout filter it will also be used by git diff...

Hah, you're totally right, forgot to install it after running the filter-branch. Ok, this script is awesome!

from nbstripout.

kynan avatar kynan commented on July 23, 2024

The --index-filter is faster since it does not need to check out the work tree but only works with the index directly. But I guess in your case speed was not a concern!

from nbstripout.

craffel avatar craffel commented on July 23, 2024

Yes, took about 10 seconds :) thanks for your help

from nbstripout.

kynan avatar kynan commented on July 23, 2024

Pleasure. Close the issue if you're satisfied! :)

from nbstripout.

kynan avatar kynan commented on July 23, 2024

@belteshassar Good point adding it to the README, will do that.

from nbstripout.

AntoineGlacet avatar AntoineGlacet commented on July 23, 2024

Hi, I am a bit late to the party, but can we update this to use git filter-repo instead of git filter-branch?
It is now the git-recommended best practice to rewrite history (https://github.com/newren/git-filter-repo/).

I am not very familiar with it though so can't really tell you the exact command.

from nbstripout.

kynan avatar kynan commented on July 23, 2024

Thanks, good suggestion! I hadn't heard for git filter-repo before and it was new to me that it's now recommended over git filter-branch. I'm reluctant to make this change though since

  1. I'm not familiar with git filter-repo and have never used it
  2. It's not a built-in command for git and needs to be installed separately afaict

If anyone has experience and wants to send a PR I'm happy to consider!

from nbstripout.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.