Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore whitespace for only certain file extensions when merging

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
like image 904
Alexander Mills Avatar asked Mar 07 '18 02:03

Alexander Mills


People also ask

How does git merge handle whitespace differences between versions?

The git-merge docs explain quite well how this will affect your merge: If their version only introduces whitespace changes to a line, our version is used; If our version introduces whitespace changes but their version includes a substantial change, their version is used; Otherwise, the merge proceeds in the usual way.

What is Git 3 way merge?

3-way merges use a dedicated commit to tie together the two histories. The nomenclature comes from the fact that Git uses three commits to generate the merge commit: the two branch tips and their common ancestor.

What is merge made by the recursive strategy?

Recursive is the default merge strategy when pulling or merging one branch. Additionally this can detect and handle merges involving renames, but currently cannot make use of detected copies. This is the default merge strategy when pulling or merging one branch.

How does git decide to merge?

Instead, Git uses an algorithm to determine what bits of a file have changed and if any of those bits could represent a conflict. For simple text files, Git uses an approach known as the longest common subsequence algorithm to perform merges and to detect merge conflicts.


1 Answers

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:]]+"
like image 56
VonC Avatar answered Oct 13 '22 20:10

VonC