Search code examples
gitgit-pull

Empty merge conflicts


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.


Solution

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