Reset files in Git which show as modified but have no changes according to “git diff”

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












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






share|improve this question

















  • 1




    what's wrong with git checkout -- .?
    – Vogel612♦
    Feb 8 at 15:26










  • That would reset all of the files. I only wanted to reset the ones that show as changed but have no changes according to git diff. I'd like to maintain the ones that have genuine changes. This is definitely not explained well enough in my post, though.
    – Geesh_SO
    Feb 8 at 15:39






  • 2




    Same question on stackoverflow: stackoverflow.com/q/48688292/7552
    – glenn jackman
    Feb 8 at 15:44










  • I wonder if you really need to parse the git status output; does git diff --name-only list such files?
    – Mathias Ettinger
    Feb 8 at 16:51
















up vote
3
down vote

favorite












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






share|improve this question

















  • 1




    what's wrong with git checkout -- .?
    – Vogel612♦
    Feb 8 at 15:26










  • That would reset all of the files. I only wanted to reset the ones that show as changed but have no changes according to git diff. I'd like to maintain the ones that have genuine changes. This is definitely not explained well enough in my post, though.
    – Geesh_SO
    Feb 8 at 15:39






  • 2




    Same question on stackoverflow: stackoverflow.com/q/48688292/7552
    – glenn jackman
    Feb 8 at 15:44










  • I wonder if you really need to parse the git status output; does git diff --name-only list such files?
    – Mathias Ettinger
    Feb 8 at 16:51












up vote
3
down vote

favorite









up vote
3
down vote

favorite











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






share|improve this question













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








share|improve this question












share|improve this question




share|improve this question








edited Feb 15 at 16:20









chicks

1,3792818




1,3792818









asked Feb 8 at 15:04









Geesh_SO

1183




1183







  • 1




    what's wrong with git checkout -- .?
    – Vogel612♦
    Feb 8 at 15:26










  • That would reset all of the files. I only wanted to reset the ones that show as changed but have no changes according to git diff. I'd like to maintain the ones that have genuine changes. This is definitely not explained well enough in my post, though.
    – Geesh_SO
    Feb 8 at 15:39






  • 2




    Same question on stackoverflow: stackoverflow.com/q/48688292/7552
    – glenn jackman
    Feb 8 at 15:44










  • I wonder if you really need to parse the git status output; does git diff --name-only list such files?
    – Mathias Ettinger
    Feb 8 at 16:51












  • 1




    what's wrong with git checkout -- .?
    – Vogel612♦
    Feb 8 at 15:26










  • That would reset all of the files. I only wanted to reset the ones that show as changed but have no changes according to git diff. I'd like to maintain the ones that have genuine changes. This is definitely not explained well enough in my post, though.
    – Geesh_SO
    Feb 8 at 15:39






  • 2




    Same question on stackoverflow: stackoverflow.com/q/48688292/7552
    – glenn jackman
    Feb 8 at 15:44










  • I wonder if you really need to parse the git status output; does git diff --name-only list such files?
    – Mathias Ettinger
    Feb 8 at 16:51







1




1




what's wrong with git checkout -- .?
– Vogel612♦
Feb 8 at 15:26




what's wrong with git checkout -- .?
– Vogel612♦
Feb 8 at 15:26












That would reset all of the files. I only wanted to reset the ones that show as changed but have no changes according to git diff. I'd like to maintain the ones that have genuine changes. This is definitely not explained well enough in my post, though.
– Geesh_SO
Feb 8 at 15:39




That would reset all of the files. I only wanted to reset the ones that show as changed but have no changes according to git diff. I'd like to maintain the ones that have genuine changes. This is definitely not explained well enough in my post, though.
– Geesh_SO
Feb 8 at 15:39




2




2




Same question on stackoverflow: stackoverflow.com/q/48688292/7552
– glenn jackman
Feb 8 at 15:44




Same question on stackoverflow: stackoverflow.com/q/48688292/7552
– glenn jackman
Feb 8 at 15:44












I wonder if you really need to parse the git status output; does git diff --name-only list such files?
– Mathias Ettinger
Feb 8 at 16:51




I wonder if you really need to parse the git status output; does git diff --name-only list such files?
– Mathias Ettinger
Feb 8 at 16:51










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










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





share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f187095%2freset-files-in-git-which-show-as-modified-but-have-no-changes-according-to-git%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    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





    share|improve this answer

























      up vote
      2
      down vote



      accepted










      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





      share|improve this answer























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        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





        share|improve this answer













        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






        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Feb 10 at 5:46









        janos

        95.6k12120343




        95.6k12120343






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f187095%2freset-files-in-git-which-show-as-modified-but-have-no-changes-according-to-git%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Chat program with C++ and SFML

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            Will my employers contract hold up in court?