Reset files in Git which show as modified but have no changes according to âÂÂgit diffâÂÂ
Clash 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
performance bash shell git
add a comment |Â
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
performance bash shell git
1
what's wrong withgit 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 togit 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 thegit status
output; doesgit diff --name-only
list such files?
â Mathias Ettinger
Feb 8 at 16:51
add a comment |Â
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
performance bash shell git
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
performance bash shell git
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 withgit 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 togit 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 thegit status
output; doesgit diff --name-only
list such files?
â Mathias Ettinger
Feb 8 at 16:51
add a comment |Â
1
what's wrong withgit 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 togit 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 thegit status
output; doesgit 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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered Feb 10 at 5:46
janos
95.6k12120343
95.6k12120343
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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; doesgit diff --name-only
list such files?â Mathias Ettinger
Feb 8 at 16:51