The expression for a notebook is quite verbose so a small change in the frontend appearance can result in many changes to the expression. It is not easy to understand what has changed by looking at the textual differences. Trying to resolve merge conflicts is very painful. If you want version control use packages or save notebooks as .m / .wl and version control them.
Thank you for your wonderful comments and sharing your valuable experience. But your method seems quite complicated. I need to learn it. If there is any problem, I will give follow-up feedback.
I try to use the code snippet above from another new notebook to deal with the notebook in the attachment. But what is generated is a corrupt file with syntax errors.
Something was changed in the exporter to the "Package" format since developing the original implementation. With Mathematica 8.0.4 it still works correctly, but not with the latest versions. I’ll inverstigate and try to fix it this week.
The immediate reason for the syntax error is that in recent Mathematica versions Export as "Package" writes Pattern[sym,obj] as sym:obj instead of Pattern[sym,obj] as it was in earlier versions. The syntax sym:obj is recognized by the FrontEnd as a syntax error when opening the file as Notebook. Exactly the same problem is with Put. Hence in the recent versions they broken the compatibility between Get and Put. I think that it is a bug.
Here is an improved version of CleanNotebookForGit with a fix for above-described Pattern export bug. Another improvement is that it doesn’t clean up options inside of BoxData, where this can cause problems:
How can I script the above code snippet instead of running it from inside Mathematica?
Why not make it a customized package, to facilitate loading and using it?
Some advice about the default arguments: Set the inputFile’s default value to the notebook name where this function is called, and set outputFile’s default value to something meaningful ending with git or like.
and 3. The function is quite simple, so everyone can easily adapt it to suit their goals.
P.S. I just discovered that in the WFR is available ResourceFunction["SaveReadableNotebook"] which at first glance looks like a more generic and advanced version of CleanNotebookForGit.
Thank you very much for telling me this function. The biggest problem is that the first execution of this function is time-consuming because of the network query. But downloading the notebook and deploying it locally on the computer can solve this problem, as shown below:
FilePrint::badfile: The specified argument, FunctionRepository`$31760fa6b86145928a98537accf1a6b8`SaveReadableNotebook[nb,FileNameJoin[{/home/werner,readable.nb}]], should be a valid string or File.
As you can see, the In/Out cell numbering has been growing. This information is not conducive to git version control. It would be better if the numbers of these cells don’t be tracked in the result file.