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
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
which would limit the effect of the change to the execution of the
Lastly, some minor style issues:
$gitDiffStr variable is pointless, you can simply inline it.
== operator within a
[ ... ] is undocumented. Use
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
gitDiff=$(git diff "$path")
if [ "$gitDiff" = "" ]; then
echo git checkout "$currentFile"
git checkout "$currentFile"