Is there any way to make Git ignore changes related to spaces or line breaks?

Question:

I have a refactoring habit. If I see something missing or excessively word-wrapped, the first thing I do is fix it.

I'm using Visual Studio Code in a project and I use GIT to do version control. Those files that I usually break a line or space to correct some formatting end up filling the notification tab of changes in the project.

I would like to know if there is any way to make GIT ignore insignificant changes like line breaks or adding spaces (not between one character or another, but white space).

I mean, I want GIT to disregard changes like these when listing files for commit with git status .

Update:

There's a blessed being working on the same project as me who loves to use 2 spaces, while I like to use 4 spaces per tab.

I would like to know if there is a way to ignore this also by the GIT.

Answer:

I mean, I want GIT to disregard changes like these when listing files for commit with git status.

I understand that you would like this behavior after doing some kind of merge with a branch where the space pattern used is different from yours.

While it is possible to do this "git status" by ignoring space differences, using a git diff (untested example: git diff -w | git apply --cached --ignore-whitespace ), troubleshooting your issue starts at do time. merge the branches.

Unfortunately it is not possible to simply ignore file spaces. A file with 2 spaces is different from a file with 4 spaces inside git as they generate different hashing. When git sees this situation on a merge, it's up to it to try to merge or show the problem (in the form of a conflict).

However, you can have it resolve this merge for you automatically, "ignoring" this space problem. Note that in this case you're not really ignoring it, you're just telling git what to do in this case.

For this, at merge time, use the -Xignore-space-change flag:

git merge -s recursive -Xignore-space-change

The -Xignore-space-change here is explained in the following way by the documentation on how it affects your merge:

  • If his version only introduces space changes on the line, his version is used.
  • If your version introduces space changes but his version includes a substantial amount of changes, his version is used.
  • Otherwise, the merge proceeds normally.

Lastly

Furthermore, if your friend likes 2 spaces and you like 4 spaces, and you can't come to an agreement, you might consider switching to tab-by-tab, then each of you choose the tab size by the IDE, solving better this question of how they prefer to view the code.

Scroll to Top