what causes submodule conflicts in git, and how should they be resolved?

user561638 picture user561638 · Jul 14, 2011 · Viewed 14.3k times · Source

We are using submodules and we are new to git.

We often see merge conflicts for the submodules themselves, no files are conflicted, just the submodule. There are multiple versions listed in the output of git submodule summary. We resolve them by running git add <submodule> in the superproject.
But today we had a developer lose a commit of the submodule when she resolved the conflict in this manner.
Does running a git add choose the remote version? Shouldn't the contents of the submodule get merged? If she made changes in the submodule and committed them (which I see), then why would that commit disappear after she ran the pull and resolved the conflict?

Answer

Andrew Wagner picture Andrew Wagner · May 19, 2014

Your local submodule and the remote submodule have diverged.

git checkout --theirs submodulename

or for your version:

git checkout --ours submodulename

and then commit the changes with git add and commit the changes.

Note: Your shell may add a trailing slash to the submodulename if you tabcomplete, since it is also a subdirectory. If so, you need to delete it or you'll get:

error: pathspec 'submodulename/' did not match any file(s) known to git.