4

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.

Shrijit Basak
  • 307
  • 1
  • 13

2 Answers2

4

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.

mnestorov
  • 4,116
  • 2
  • 14
  • 24
  • I think there is an issue here that if I have 1 file with some changes and another file which has new line and other white space changes then this might still show me both the files. But Yes! I find this method much better, with just a little tweaking we can create a git status wrapper – Shrijit Basak Jul 29 '20 at 08:11
  • Hm interesting, on my machine, if I have a file only with new lines and another with trailing whitespaces, the script still works fine. I guess it depends if you are changing the `diff` command. Are you specifying to it any paths? Otherwise I'm happy this does the job :) – mnestorov Jul 29 '20 at 08:20
1

You could setup a clean content filter driver as here which will automatically remove those trailing empty lines on on git diff/git commit.

https://git-scm.com/book/en/v2/images/clean.png Image from "Keyword Expansion" section of the "ProGit book"

That clean script would trim any trailing empty lines

# Delete all trailing blank lines at end of file (only).
sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba' file
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250