Is there an easy way to automatically do a git checkout on any file that only has whitespace changes? I\'m dealing with both Windows and some code generation that\'s run from Ec
Another approach is to use dos2unix command on a unix or (windows pc as a special install) to convert the line endings and commit and push
This is most likely a line ending issue; Windows uses CRLF (carriage return + line feed, or \r\n
) line endings, whereas most other systems use LF (line feed, or \n
). Luckily, Git can normalize files such that they are always stored as LF in the repo, but will be checked out as CRLF on Windows. You can do this by setting core.autocrlf
to true
on Windows:
$ git config --global core.autocrlf true
and setting core.autocrlf
to input
on Mac and Linux:
$ git config --global core.autocrlf input
More info is available here (scroll down to the section titled core.autocrlf).
To stage changes that are not just whitespace changes, you can do:
git diff -U0 -w --no-color | git apply --cached --ignore-whitespace --unidiff-zero -
Afterwards, to remove all unstaged changes (those changes that differ only in whitespace), you can do:
git checkout .
Unstage your changes by doing a git reset --mixed
and continue from the top of this answer. Note that mixed
is the default mode and can be omitted.
rumpel's answer was the right fix for me. I would just like to complete a bit :
If your changes have been pushed
Once you followed rumpel's procedure, do not push again on your branch. Github will ask you to pull before and you will get every whitespace diff again. Instead create a new branch from your current branch and push the new one :
# From $mybranch
git checkout -b $mybranch-v2
git push --set-upstream origin $mybranch-v2
Then on GitHub close and delete $mybranch
(there’s probably a smarter way but this works for me)
mybranch=master
git checkout -b tmp origin/master
# compute the non-ws diff to mybranch and apply it
git diff -U0 -w --no-color $mybranch | git apply -R --cached --ignore-whitespace --unidiff-zero -
git commit -m "non ws changes"
git reset --hard # discard all non-staged data
Your now on a new "tmp" branch. You may want to clean up (NOTE: this loses history of $mybranch
)
git checkout $mybranch
git reset --hard tmp
git branch -D tmp