1

Sometimes, when I do git pull origin master from a local branch, I get merge conflicts like:

<<<<<<HEAD

======

>>>>>>xxxxxx

How to avoid that ? Maybe it is due to some white spaces, so I tried to put a .gitattributes file containing * -whitespace but that didn't solve the problem.

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225

1 Answers1

5

In this case, you have white-space differences. Git considers whitespace differences significant. (If this were not the case, well ... imagine maintaining a program written in Whitespace, for instance.)

Your comment:

... followed by git merge -s recursive -Xignore-space-change origin/master. This worked and I got no confict this time.

confirms that the conflicts were merely with whitespace. The -X argument—I call these extended arguments, with X standing for eXtended)—ignore-space-change tells Git that, during the merge, if your change and their change are the same except for whitespace, this is not really a conflict.

The precise rules for these four extended options are described in the documentation:

ignore-space-change
ignore-all-space
ignore-space-at-eol
ignore-cr-at-eol

Treats lines with the indicated type of whitespace change as unchanged for the sake of a three-way merge. Whitespace changes mixed with other changes to a line are not ignored. See also git-diff[1] -b, -w, --ignore-space-at-eol, and --ignore-cr-at-eol.

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

Note that you can usually spell this command more simply as:

git merge -X ignore-space-change

The -s recursive is the default, and origin/master is presumably already set as the upstream of your current branch master so that this too is the default.

(The space between -X and its argument is optional, but I prefer to use it.)

torek
  • 448,244
  • 59
  • 642
  • 775
  • Thanks. Nothing happens if I simply do `git merge -X ignore-space-change`. This only prints "Already up-to-date". I have to do `git merge -X ignore-space-change origin/master`. – Stéphane Laurent Nov 02 '19 at 20:52
  • That implies that the upstream is *not* `origin/master`, but rather something else. If you like that arrangement, all you can leave out is the `-s recursive` part. (That the current branch's upstream is not `origin/master` implies in turn that the current branch is probably not `master`: I was assuming that it was. You only said "a local branch", not *which* local branch, so that last assumption on my part was probably wrong.) – torek Nov 02 '19 at 22:50