Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make git undo any whitespace-only changes?

Tags:

git

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

like image 856
Sophistifunk Avatar asked Dec 10 '12 00:12

Sophistifunk


People also ask

Does git ignore whitespace changes?

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.

How do I hide Whitespaces in github?

On github, you simply append the w=1 parameter to the URL for it to ignore whitespace.


3 Answers

If your changes are not staged

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 .

If your changes are staged

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.

like image 144
Kent Munthe Caspersen Avatar answered Oct 05 '22 22:10

Kent Munthe Caspersen


If your changes are commited

(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
like image 39
rumpel Avatar answered Oct 05 '22 21:10

rumpel


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).

like image 39
mipadi Avatar answered Oct 05 '22 22:10

mipadi