27. How to Use Git with Jupyter Notebooks for Data Projects
27.4 Notebooks and Diffs: Clear Outputs
Why. Notebooks store outputs (tables, plots) inside the .ipynb JSON. Tiny code edits create huge noisy diffs. Reviewers cannot see your real change.
What.
- Clear outputs before commit when the goal is code review.
- Or use a tool/filter later (nbdime etc.) — optional; this course stresses the clear-output habit.
- Still run notebooks locally; clearing is for what you commit, not for what you analyse.
How.
Clean commit loop
- Do analysis; keep outputs while you think.
- Before
git commit, clear outputs (Jupyter menu: Kernel / Clear, or “Clear All Outputs”). - Save the
.ipynb. git add+ commit with a clear message.- Re-run cells next session when you continue work.
Worked example A — What blows up diffs.
A seaborn chart PNG embedded in the notebook can be hundreds of KB per run. Code change was one filter line — diff looks like a novel.
Worked example B — Message after clear.
git add notebooks/01_clean.ipynb
git commit -m "Fix Pune city spelling in clean step"
Worked example C — Pair with outputs folder.
Save manager PNGs via savefig into outputs/ (Chapter 22). Commit the script/notebook logic; treat PNGs as optional artifacts.
Ravindra Bagale's Tip
Analyse karta outputs theva. Commit karta clear kara. Diff chota = review soft.