DrEsperanto
DrEsperanto

Reputation: 197

git diff --name-only with ignore space seems to be broken

I have a repository where there are many files that only have EOL whitespace changes, but some have actual changes. I am trying to craft an alias to open diffs of only the files with real changes in vim tabs, and as part of that I am running a git diff --name-only ... command to get a list of files that have changed so I can open them in tabs.

My problem is that --ignore-space-at-eol, --ignore-space-change, and --ignore-all-space seem to do nothing when combined with --name-only.

When I run the following command nothing is printed out (my_file has a CR/LF change):

git diff --ignore-space-at-eol my_file

But when I run this I get my_file printed out:

git diff --ignore-space-at-eol --name-only my_file

Is there some way to get only the names of files that have no whitespace changes? This seems like a bug to me, but I haven't been able to find a similar issue.

I am on git version 2.25.0

Upvotes: 8

Views: 1749

Answers (2)

RalfW
RalfW

Reputation: 81

I happened to stumble onto a similar problem recently and also found that using the --name-only option will not work with either --ignore-all-space or --ignore-blank-lines.

As someone in the comments pointed out this is due to --name-only only checking if a file has changed but not calculating the actual change. What I found is that you can use the --stat option instead, since this will calculate the actual changes to files.

So for getting a list of files that have changed while ignoring whitespace changes you might use something like the following:

git diff --stat --ignore-all-space --ignore-blank-lines --diff-filter=M <branchname>

Upvotes: 8

VonC
VonC

Reputation: 1323403

You will need to:

  • get the list of files first: git diff -–name-only
  • then for each file do a git diff --ignore-space-at-eol my_file

But if you want to perform the second diff only if there are actual changes, wrap that second diff with a test (as in here), using the --quiet option:

if ! git diff --quiet --ignore-space-at-eol my_file; then
    git diff --ignore-space-at-eol my_file
fi

Upvotes: 1

Related Questions