Search code examples
gitgit-submodules

How to replace a git submodule with the contents of the submodule?


On a company project we used a submodule for a while but it caused issues again and again. Our decision was to remove the submodule and copy all files of the submodule into the folder where the submodule was. (This is required because we have an old build system which requires the files in that folder.

So I made the changes:

cp -r build/sub ../sub
git rm -f build/sub
mv ../sub build/sub
rm build/sub/.git

That worked on my machine and on the build system too. But a co-worker tried to pull the changes and got: The following untracked working tree files would be overwritten by merge. After the pull we tried git fetch --all && git reset --hard origin/master, which did not fail but it did not work either. We don't have the error messages anymore but it failed to remove the build/sub folder because it was not empty but git proceeded. It copied almost all files into the build/sub folder but it did not remove the submodule itself. So the .git file was still in the build/sub folder but the subdmodule was remove from the .gitmodules file.

So... I am not sure how to continue here without breaking the repositories of my colleagues. How can I replace the submdoule with the contents of the submodule but without breaking stuff?


Solution

  • You want to use this: https://git-scm.com/docs/git-submodule#Documentation/git-submodule.txt-deinit-f--force--all--ltpathgt82308203

    It's not enough to manually delete parts of your working directory/tree (subdirectory). You must tell git that the submodule should no longer exist in the repository. So that git can abandon the submodule, and delete it.

    Just like when you add a repository: you don't copy and paste the content, you configure a submodule in git, then let git update the contents of the subdirectory.