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 Eclipse, so I get newline changes and whitespace changes that are clogging up my workflow with noise, and making it difficult to track actual changes.
Even just a nice way to report which files have real changes and which don't would be a start, rather than having to do a diff -w for each one
We use the git diff -w command to ignore all whitespace differences. It will ignore spaces at the beginning, middle, and end of lines. We use the git diff --ignore-space-at-eol command to ignore whitespace changes at the end of our lines.
On github, you simply append the w=1 parameter to the URL for it to ignore whitespace.
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.
(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
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With