221

I've got a problem, where a large commit which changes about a thousand lines of code, removing whitespace from the end of lines and removing spaces before tabs.

There are also about 50 pull requests for this project, in which all will get conflicts, when my commit is merged. Is there any way that git can be set up so that when merging future commits, it ignores conflicts where one of them is just a whitespace change?

Modifying git itself or using a third party tool isn't possible, but using a hook is fine.

1

3 Answers 3

298
 git merge -Xignore-all-space

Or (more precise)

 git merge -Xignore-space-change

should be enough to ignore all space related conflicts during the merge.

See git diff:

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

--ignore-all-space

Ignore whitespace when comparing lines.
This ignores differences even if one line has whitespace where the other line has none.

ks1322 adds in the comments a good advice:

It is worth to merge with --no-commit and review the merge before actual commit.


The OP Callum Macrae reports that, in that case, the merge proceed uninterrupted, and the trailing spaces contained in the pull request patches are applied to the local files.
However, the OP uses a pre-commit hook which takes care of said trailing spaces.
(I suppose a bit similar to this one, also referenced here).


The OP's pre-commit hook is referenced here:

In addition to removing trailing whitespace, it removes one to three spaces before tabs (I have tab width set to 4), and adds EOLs.
I've had reports that the code that adds the EOL deletes the file in windows, but haven't been able to replicate it.

26
  • 2
    From my tests so far, it looks like it removes all whitespace changes in the file. However, I am now using the pre-commit hook to remove tailing whitespace so it isn't an issue. Mar 21, 2012 at 12:19
  • 1
    @CallumMacrae: ok, and thank you for the feedback. Again, I have added your link in the answer for more visibility.
    – VonC
    Mar 21, 2012 at 12:55
  • 1
    git merge -Xignore-space-change - well, I can say for certain this did not work.... The one piece of whitespace I was trying to remove remained. Git absolutely sucks.
    – jww
    Jan 14, 2016 at 15:07
  • 1
    It is worth to merge with --no-commit and review the merge before actual commit.
    – ks1322
    May 20, 2016 at 15:34
  • 1
    -Xignore-all-space seems to do nothing about whitespace and --ignore-space-change was not recognized as an option by git
    – Preza8
    Aug 28, 2019 at 13:10
2

If you see that you have a lot of whitespace issues in a merge, you can simply abort it and do it again, this time with -Xignore-all-space or -Xignore-space-change. The first option ignores whitespace completely when comparing lines, the second treats sequences of one or more whitespace characters as equivalent.

0

I like to use meld to merge involved conflicts and there is the option to set various filter options under Meld>Preferences>TextFilters.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.