Quickly add changes to git

Hi,

I’m starting to really like nbdev only I found my self going back and forth between my git client. I wrote a method using GitPython to quickly add the changes of a particular notebook.

# export
from git import Repo
from nbdev.export import Config as nb_Config
from nbdev.export import *

def git_add(fname, commit_msg='.'):
    repo = Repo(nb_Config().nbs_path.parent)
    notebook2script(fname)
    nb = read_nb(fname)
    default = find_default_export(nb['cells'])
    py = [os.path.join(nb_Config().lib_path,*default.split('.'))+'.py',
          os.path.join(nb_Config().nbs_path,fname)
         ]
    repo.index.add(py)
    repo.index.commit(commit_msg)
    return py

This is quit handy when changing things inside a model. Now I can just add the function below and easily iterate and still save the minor changes. You do get a lot of commits but you can squash those into one later.

It doesn’t work if the notebook is not saved. Anyone knows how to save a notebook (with code)?

What do you think?

3 Likes

Great idea!

I keep the following cell at the end of the “Committing” instructions section of the index.ipynb / README notebook:

#hide
# Heck, if you're feeling really lucky, you could even run the following
# to blindly commit and push.

# Did you save this notebook?

! make >/dev/null 2>&1
! git commit -am '.' >/dev/null 2>&1
! git status >/dev/null 2>&1
! git push >/dev/null 2>&1

I try to only keep it there, which helps me remember to save the notebook I’m actually interested in committing.

@oguiza made a script to save your notebook inside a notebook: