longbow
longbow

Reputation: 372

Git diff: How to ignore starting space of an empty line?

I'm an iOS developer, when I press enter, xcode automatically indent the new line with 4 spaces, that's convenient for developing.

But when it comes to using git diff, every empty line will be marked with red color.

This can be annoying in team development.

So how to deal with it? Thanks in advance!

Upvotes: 4

Views: 2013

Answers (2)

VonC
VonC

Reputation: 1329322

With Git 2.25 (Q1 2020), three+ years later, the "diff" machinery learned not to lose added/removed blank lines in the context when --ignore-blank-lines and --function-context are used at the same time.

So your intermediate 4 empty lines won't show up.
But in the context of functions, they might.

See commit 0bb313a (05 Dec 2019) by René Scharfe (rscharfe).
(Merged by Junio C Hamano -- gitster -- in commit f0070a7, 16 Dec 2019)

xdiff: unignore changes in function context

Signed-off-by: René Scharfe

Changes involving only blank lines are hidden with --ignore-blank-lines, unless they appear in the context lines of other changes.

This is handled by xdl_get_hunk() for context added by --inter-hunk-context, -u and -U.

Function context for -W and --function-context added by xdl_emit_diff() doesn't pay attention to such ignored changes; it relies fully on xdl_get_hunk() and shows just the post-image of ignored changes appearing in function context.

That's inconsistent and confusing.

Improve the result of using --ignore-blank-lines and --function-context together by fully showing ignored changes if they happen to fall within function context.

Upvotes: 1

CodeWizard
CodeWizard

Reputation: 142632

Use this when using diff

git diff -w // (--ignore-all-space)

You can create an alias for this so you will not have to type it every time.

git config --global alias.NAME 'diff --ignore-space-change'

git diff

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

--ignore-blank-lines
Ignore changes whose lines are all blank.

Upvotes: 4

Related Questions