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
Keep outputs while you analyse, but clear all outputs before you commit the notebook. A small diff lets a reviewer see your real code change instead of megabytes of printed tables and charts. Save charts for managers into the outputs/ folder with savefig instead.
Ravindra Bagale's Tip – मराठी
Analysis करताना outputs राहू द्या, पण notebook commit करण्याआधी सगळे outputs clear करा. छोट्या diff मुळे reviewer ला मेगाबाइट्सच्या print केलेल्या tables आणि charts ऐवजी तुमचा खरा code बदल दिसतो. Managers साठीचे charts त्याऐवजी savefig ने outputs/ folder मध्ये जतन करा.
Ravindra Bagale's Tip – हिंदी
Analysis करते समय outputs रहने दो, लेकिन notebook commit करने से पहले सारे outputs clear करो। छोटे diff से reviewer को मेगाबाइट्स की print की हुई tables और charts की जगह आपका असली code बदलाव दिखता है। Managers के लिए charts को इसकी जगह savefig से outputs/ folder में सेव करो।