{"thread":{"id":"40056","subject":"Merging after directory got turned into submodule","startedAt":"2015-08-11T13:02:41Z","lastAt":"2015-08-11T13:25:31Z","messageCount":2,"participants":["Martin von Gagern","John Keeping"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"267825","messageId":"55C9F271.30008@gmx.net","threadId":"40056","inReplyTo":null,"subject":"Merging after directory got turned into submodule","fromName":"Martin von Gagern","fromEmail":"martin.vgagern@gmx.net","sentAt":"2015-08-11T13:02:41Z","receivedAt":"2015-08-11T13:02:41Z","isPatch":false,"sender":{"key":"martin.vgagern@gmx.net","avatar":"https://avatars.githubusercontent.com/u/190759?v=4"},"body":"Hi!\n\nI've noticed that if I turn a subdirectory into a submodule, I'm having\nsevere trouble merging branches from before that change, even if they\ndon't modify that subdirectory at all.\n\nI've posted this problem on Stack Overflow and started a bounty for it.\nSee http://stackoverflow.com/q/31821219/1468366. So far I haven't\nreceived an answer, so I decided to ask here as well.\n\nHere is an example.\n\n# Create one project, to be used as a subproject later on\ngit init a\ncd a\necho aaa > aa\ngit add -A\ngit commit -m a1\ncd ..\n\n# Create a second project, containing a as a normal directory initially\ngit init b\ncd b\nmkdir a b\necho aaa > a/aa\necho bbb > b/bb\ngit add -A\ngit commit -m b1\n\n# Replace directory with submodule\ngit rm -r a\ngit submodule add ../a a\ngit commit -m b2\n\n# Create feature brach starting at version without submodule\ngit submodule deinit .  # will error if I don't do this\ngit checkout -b branch HEAD^\necho abc > b/bb\ngit commit -a -m b3\n\n# Try to merge the feature branch\ngit checkout master\ngit merge branch\n\nThis prints an error message:\n\n> CONFLICT (file/directory): There is a directory with name a in branch.\n> Adding a as a~HEAD\n> Automatic merge failed; fix conflicts and then commit the result.\n\nI get the same error if I do a git submodule update --init before the\ngit merge branch. I don't see any a~HEAD anywhere, neither in my\ndirectory tree nor in the output from git status, which reads like this:\n\n> On branch master\n> You have unmerged paths.\n>   (fix conflicts and run \"git commit\")\n>\n> Changes to be committed:\n>\n>     modified:   b/bb\n>\n> Unmerged paths:\n>   (use \"git add <file>...\" to mark resolution)\n>\n>     added by us:     a\n\nIf I do git add a as suggested, I get another error:\n\n> error: unable to index file a\n> fatal: updating files failed\n\nIf I do git submodules update --init just before the merge, then I can\ndo git add a successfully. But if I forget to do so, and then try doing\nthat after the merge, I receive this error message:\n\n> Submodule 'a' (…/a) registered for path 'a'\n> Skipping unmerged submodule a\n\nHow do I recover from this situation? Something other than git merge\n--abort, since I'd like to use it for things like git rebase as well,\nand since in some scenarios (don't know how to reproduce) I couldn't\neven abort the merge cleanly, and had to do a hard reset instead.\n\nHow can I avoid it in the first place? Is there some magic setting which\nmakes git do the right thing with submodules vs. directories during\nmerges, so that I don't have to manually post-process a merge which only\nmodifies files unrelated to the submodules?\n\nIf there is no easy way to avoid this, do you think I should file a bug\nreport for this? After all, replacing a subdirectory by a submodule\nshouldn't be that rare, and neither should be merges across such a\nchange. So in my opinion, git should be able to cope with this.\n\nGreetings,\n Martin von Gagern\n"},{"id":"267829","messageId":"20150811132530.GG30507@serenity.lan","threadId":"40056","inReplyTo":"55C9F271.30008@gmx.net","subject":"Re: Merging after directory got turned into submodule","fromName":"John Keeping","fromEmail":"john@keeping.me.uk","sentAt":"2015-08-11T13:25:31Z","receivedAt":"2015-08-11T13:25:31Z","isPatch":false,"sender":{"key":"john@keeping.me.uk","avatar":"https://avatars.githubusercontent.com/u/1702081?v=4"},"body":"On Tue, Aug 11, 2015 at 03:02:41PM +0200, Martin von Gagern wrote:\n> I've noticed that if I turn a subdirectory into a submodule, I'm having\n> severe trouble merging branches from before that change, even if they\n> don't modify that subdirectory at all.\n> \n> I've posted this problem on Stack Overflow and started a bounty for it.\n> See http://stackoverflow.com/q/31821219/1468366. So far I haven't\n> received an answer, so I decided to ask here as well.\n> \n> Here is an example.\n> \n> # Create one project, to be used as a subproject later on\n> git init a\n> cd a\n> echo aaa > aa\n> git add -A\n> git commit -m a1\n> cd ..\n> \n> # Create a second project, containing a as a normal directory initially\n> git init b\n> cd b\n> mkdir a b\n> echo aaa > a/aa\n> echo bbb > b/bb\n> git add -A\n> git commit -m b1\n> \n> # Replace directory with submodule\n> git rm -r a\n> git submodule add ../a a\n> git commit -m b2\n> \n> # Create feature brach starting at version without submodule\n> git submodule deinit .  # will error if I don't do this\n> git checkout -b branch HEAD^\n> echo abc > b/bb\n> git commit -a -m b3\n> \n> # Try to merge the feature branch\n> git checkout master\n> git merge branch\n> \n> This prints an error message:\n> \n> > CONFLICT (file/directory): There is a directory with name a in branch.\n> > Adding a as a~HEAD\n> > Automatic merge failed; fix conflicts and then commit the result.\n> \n> I get the same error if I do a git submodule update --init before the\n> git merge branch. I don't see any a~HEAD anywhere, neither in my\n> directory tree nor in the output from git status, which reads like this:\n> \n> > On branch master\n> > You have unmerged paths.\n> >   (fix conflicts and run \"git commit\")\n> >\n> > Changes to be committed:\n> >\n> >     modified:   b/bb\n> >\n> > Unmerged paths:\n> >   (use \"git add <file>...\" to mark resolution)\n> >\n> >     added by us:     a\n> \n> If I do git add a as suggested, I get another error:\n\n\"git reset a\" works for me (i.e. set a to what it was before merging).\nIf you then commit and check \"git ls-tree HEAD\" it shows \"a\" is still a\nsubmodule.\n"}]}