Search code examples
javagitnewlinegit-commit

Make git ignore consecutive newlines


I'd like to track in git just the code (java), I'd like to have it ignore additions of multiple newline from previous commit (if no other code has changed) ex:

I'd like this

class MyApp{
    public static void main(String[] args) {
        if (args.length > 0) {
            for(String s : args){
                system.err.println("arg is "+s+"\n");
                }//end-for
            }//end-if
        }//end-met
    }//end-class

And this:

class MyApp{


    public static void main(String[] args) {


        if (args.length > 0) {
            for(String s : args){

                system.err.println("arg is "+s+"\n");

                }//end-for
            }//end-if


        }//end-met



}//end-class
    

to be considered the same (code should be seen as different only if non-newline character is added)

The goal of doing this is avoid replica of the same file when only noise has been added (if I search the file in git I'd like to get only when "actual" code changes has happened)


Solution

  • Git doesn't provide a way to ignore changes to a tracked file.

    If you want to format the text before committing it, you can use a clean filter (specified in .gitattributes with the filter attribute) to automatically format it in whatever way you want. You can see the documentation how to do this in the gitattributes(7) manual page. Otherwise, there's no way to accomplish your goal with Git.