Reset files in Git which show as modified but have no changes according to “git diff” This seems to be working but also is quite horrendously slow and seems a little hacky to me.

IFS=$'\n'

for currentFile in $(git status | grep "modified:" | cut -c 14-)
do
    gitDiff=$(git diff "$currentFile")
    gitDiffStr="[$gitDiff]"
    if [ "$gitDiffStr" == "[]" ]
    then
        echo match
        echo git checkout "$currentFile"
        git checkout "$currentFile"
    fi
done
The slowness

The slowness most probably comes from executing multiple git diff commands.
A faster way might be using a programming language with a Git library that would let you run a single git diff command,
and iterate over the entries of the diff to find the empty ones.

The hacky part

This is hacky:

git status | grep "modified:" | cut -c 14-


The output of the git status is not API,
and may also be subject to aliases and user settings.
It's not safe to use.
When parsing the output of Git commands,
look for a --porcelain option to make it safe.

Another hacky part is changing the value of IFS.
It's not a huge problem,
but it's good to minimize the scope of the change,
typically by using it in the form of IFS=... cmd,
which would limit the effect of the change to the execution of the cmd command.

Lastly, some minor style issues:


The $gitDiffStr variable is pointless, you can simply inline it.
The == operator within a [ ... ] is undocumented. Use = instead.


Alternative implementation

Applying the above suggestions, this is less hacky, and probably just as slow:

git status --porcelain -z | grep -z '^ M' | while IFS= read -r -d '' path0; do
    path=${path0:3}
    gitDiff=$(git diff "$path")
    if [ "$gitDiff" = "" ]; then
        echo git checkout "$currentFile"
        git checkout "$currentFile"
    fi
done