10

When resolving a conflict in a file I can

git checkout --ours filename

and then commit the file. This will resolve the conflict. However,

git checkout --ours submodule

doesn't seem to work. The reference commit for the submodule doesn't change.

What would be the equivalent of git checkout --ours filename when resolving conflicts in submodule references?

Greg
  • 8,230
  • 5
  • 38
  • 53
  • This was probably an issue with git 1.7.1. Among the tree alternatives (`-2`, `--ours` and `HEAD`) only `git checkout HEAD submodule` does what it supposed to do. Maybe all three would work on a newer version of git. – Greg Dec 17 '16 at 20:43

1 Answers1

5

Considering your next question, you can try a checkout of one of the three stages for your submodule:

git checkout -1 -- submodule # common ancestor
git checkout -2 -- submodule # source
git checkout -3 -- submodule # destination or MERGE_HEAD

Once a gitlink of a submodule has been changed, don't forget a git submodule update, to refresh its content.

The OP Amiramix refers to this quora answer, where Berk D. Demir, Production Engineer at Facebook, adds:

git checkout -1 file

...will checkout the file from the ancestor of two branches.
This is neither "ours" nor "theirs". It's the version of the file right before these two branches diverged. Quite handy.

As you can guess, parameter -2 is for the version from HEAD and -3 is version from MERGE_HEAD accordingly.

Another way to reach these files through all other git commands is to use the symbolic-refs of commits.
git checkout MERGE_HEAD -- file gives the same effect with --theirs or -3.

Another handy syntax when dealing with merge conflicts is the colon-stage-colon prefix to path. git show :3:file will show the file (not the diff) from MERGE_HEAD.

A tiny cheat sheet:

-1 == $(git merge-base HEAD MERGE_HEAD)
-2 == --ours == HEAD
-3 == --theirs == MERGE_HEAD
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • So, would `git checkout -2 -- submodule` be equivalent of "ours" and `git checkout -3 -- submodule` be equivalent of "theirs" of a normal file merge? Also, I am only merging, I don't need the submodule to be checked out or updated to that specific commit. Can I ignore `git submodule update` and just commit merged changes? – Greg Dec 15 '16 at 07:33
  • @Amiramix yes to both questions. – VonC Dec 15 '16 at 07:34
  • OK, thanks, will test that later and accept if it works. – Greg Dec 15 '16 at 07:37
  • I am testing but this doesn't seem to work.`git checkout -1 -- submodule` returns `unknown switch \`1'`. `git checkout -2` and `git checkout -3` doesn't return any error but `git diff` still shows three commits in the unmerged path (theirs, ours, and checked out locally). I am using git 1.7.1 (stuck with Centos6), could this be the reason? – Greg Dec 17 '16 at 20:13
  • @Amiramix possibly. I haven't used an 1.7 in years. I would recommend compiling a 2.11 on Centos6 (https://git-scm.com/book/en/v2/Getting-Started-Installing-Git#Installing-from-Source, dependencies listed in http://www.linuxfromscratch.org/blfs/view/svn/general/git.html) – VonC Dec 17 '16 at 20:15
  • I just found and I am testing this answer https://www.quora.com/In-a-git-merge-conflict-how-do-I-tell-git-that-for-files-X-Y-and-Z-I-want-it-to-screw-the-local-changes-and-simply-overwrite-with-the-version-being-pulled-in and looks like the equivalent `git checkout HEAD submodule` and `git checkout MERGE_HEAD submodule` works in 1.7.1. Can you please add it to the answer and I will accept. – Greg Dec 17 '16 at 20:33