Search code examples
gitgit-status

git status to ignore newline and whitespace changes


I am currently maintaining my notes in markdown files, but the editor I use(marktext) adds new empty lines if I open an existing file.

This addition of new empty lines get tracked by git and git keeps showing me these files as a modified files. Is there a way to configure git to not track these kinds of changes like new line and trailing whitespace while doing git status? Because when I do git diff --ignore-all-space --ignore-blank-lines <path-to-file> for these particular files, it shows no changes at all.

Thanks in advance.


Solution

  • I have no idea how to configure git to do what you want. Maybe it's not possible (but I'm sure people will prove me wrong). But at least you can hack it a little bit and have what you want.

    You can create a small script which checks if the git diff --ignore-all-space --ignore-blank-lines will return nothing or something, then, either output the standard nothing to commit, working tree clean string, or do a real git status.

    #!/bin/bash
    
    diff=$(git diff --ignore-blank-lines --ignore-all-space)
    
    if [[ -z $diff ]];
    then
        echo -e "On branch $(git branch --show-current)\nnothing to commit, working tree clean"
    else
        git status
    fi
    

    Then you can call this script like a git command if you put it in your global/local .gitconfig

    # git config stuff
    [alias]
        nw-status = "!p() { bash ~/path/to/script/git-nw-status.sh ; }; p"
    

    Note that this is NOT exactly what you want. If you DO have changes that are non-whitespace non-newline, then you will see git status but by checking with git diff you will also have to use the additional commands for ignore whitespace and newlines. This might be an inconvenience to you.