Search code examples
regexgitbfg-repo-cleaner

Removing multiple newline characters from git history


I was trying to clean my repo from multiple empty lines in code (more than 4) with bfg repo cleaner. I tried various regular expressions, but didn't come up with a solution. The closest result I achieved was simply removing all new lines from a file.

Any ideas how I can do this with bfg or any other tool?


Solution

  • You can use this regular expression:

    (\r?\n){4,}
    

    And replace it with this

    \n
    
    • It will accept the UNIX and the Windows linebreak (\r?\n).
    • It matches if at least 4 line endings are consecutive (without spaces on each of the lines)
    • All the consecutive linebreak characters are replaced by a single UNIX linebreak (\n)

    Here is an executable example:

    var text = document.getElementById("main").innerHTML; // read HTML because it is easier to write linebreaks in HTML
    var regex = /(\r?\n){4,}/g;
    var replacement = "\n";
    
    var result = text.replace(regex, replacement);
    console.log(text);
    console.log(result);
    <div id="main">
    
    
    
    
    
    
    
    
    line1
    
    
    
    
    line2
    
    
    line3
    
    
    
    
    
    </div>