12

I have this command

git merge -Xignore-all-space origin/dev

scares me a little bit b/c I am afraid of merging a file where whitespace matters. Is there a way to limit it to certain files, something like this:

git merge -Xignore-all-space *.js origin/dev
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • If the question is not clear, whitespace matters for some languages, but not for others. I just want to ignore whitespace changes for languages where where whitespace is not that important. – Alexander Mills May 09 '18 at 21:28

1 Answers1

10

Anytime you want to set a configuration per file extension, a good place to start is gitattributes.
In a .gitattributes files, you can set a directive per file, or per file extension.

However, the *.js -whitespace I mentioned in 2009 might not apply during a merge.

Since ignore-all-space is first a git diff option, you might need to set up a diff driver in a .gitattributes (again, only for *.js files), emulating --word-diff-regex

Use <regex> to decide what a word is, instead of considering runs of non-whitespace to be a word

Every non-overlapping match of the is considered a word. Anything between these matches is considered whitespace and ignored(!) for the purposes of finding differences.

You may want to append |[^[:space:]] to your regular expression to make sure that it matches all non-whitespace characters. A match that contains a newline is silently truncated(!) at the newline.

For example (to be tweaked in order to match what you need in a javascript file for a diff without spaces)

*.js    diff=js
[diff "js"]
    wordRegex = "[^[:space:]]+"
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • thanks, can you show what the `git` command at the command line would actually look like in full? – Alexander Mills May 09 '18 at 23:55
  • @AlexanderMills It is simpler to edit directly a `.gitattributes` file in the folder where the *.js files reside. Then test a merge (without completing it) to see if whitespace-only conflicts are ignored or not. – VonC May 10 '18 at 00:07