From a useless Git Diff to a useful one
Imagine you have a file with 500 lines of code and you changed the indentation from tabs to spaces in more than 200 lines and besides that you changed the feature. Doing a normal git diff it will be useless because you will get a lot of diffs from the indentation changes.
But there is a way to make it useful, you just need to add some options to the git diff command and you are done:
Git version <= 1.8.3.4:
git diff --ignore-space-at-eol -b -w [commit] ...
Git version >= 1.8.4:
git diff --ignore-space-at-eol -b -w --ignore-blank-lines [commit] ...
See the options definition below:
--ignore-space-at-eol
Ignore changes in whitespace at EOL.
-b
--ignore-space-change
Ignore changes in amount of whitespace. This ignores whitespace at line end, and considers all other sequences of one or more whitespace characters to be equivalent.
-w
--ignore-all-space
Ignore whitespace when comparing lines. This ignores differences even if one line has whitespace where the other line has none.
[git version 1.8.4+]--ignore-blank-lines
Ignore changes whose lines are all blank.
Written by Daniel Gomes
Related protips
11 Responses
Great tip! Although --ignore-blank-lines
throws an error and I can't find it in the git spec?
error: invalid option: --ignore-blank-lines
Edit: It appears my git version is not up to date! (also just seen @danielcsgomes reply!)
The --ignore-blank-lines option is only available on Git version 1.8.4.
Every time you commit trailing spaces a panda dies. =(
Is -w
a particularly good idea? Wouldn't it silently treat doSomething(withThis)
and do Something(with This)
as equivalent?
I don't think I'd use this approach with python code...
This is wrong! if you ever, EVER, change indentation or trim a file, it should be ALWAYS on a separate commit.
- change ONLY white space. (stash anything else)
- run a diff with -b (or add all those fancy options) and see if it shows no changes.
- commit with comment "whitespace"
- push.
- continue with your work.
(edit: this was a numbered list, but the site changed it to bullet points.... )
@gcb this pro-tip is just about the git diff, not a strategy on how to commit/push this kind of situation. If that was the case the best approach IMO is the one you mentioned ;)
@alecb I guess it will depend on your purpose and situation and of course this is not a straight command, you should mix the options depending on your needs!
@joshribakoff Tested it, and it seems to do just that:
$ git diff
diff --git a/file b/file
index 5271a52..f5ac887 100644
--- a/file
+++ b/file
@@ -1 +1 @@
-test123
+test 123
(END)
$ git diff -w
(END)
The options do not make sense to me:
- -b is synonymous to --ignore-space-change
- -w is synonymous to --ignore-all-space
If I understand the official documentation at http://git-scm.com/docs/git-diff correctly, -b should be a superset of --ignore-space-at-eol and -w in turn is a superset of -b. In that case, the command git diff --ignore-space-at-eol -b -w [commit] ...
would be equivalent to simply git diff -w [commit] ...
. And I agree with @alecb that -w is a bad idea. Also untested, but I do understand the documentation such that it would regard "test123" and "test 123" as equivalent.