72

I have a git submodule that I would like to become part of my main project (since I have a lot of project specific code that will go into the submodule).

So I'd like to remove the git references to the submodule and add the files to my main git repository.

But how ?

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
Askbar
  • 819
  • 1
  • 7
  • 11

3 Answers3

137

You must remove the gitlink entry in your index:

mv subfolder subfolder_tmp
git submodule deinit subfolder
git rm --cached subfolder
mv subfolder_tmp subfolder
git add subfolder

Replace subfolder with the name of the folder for your submodule, and make sure to not add any trailing slash.

This is what I detail in "Remove a Git submodule?" and "un-submodule a git submodule".

The --cached option allows you to keep the subfolder content in your disk... except git submodule deinit would already have removed that content anyway. Hence the mv part.

You then can add and commit that subfolder.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    But ```git submodule deinit subfolder``` removes the subfolder content from the disk. The ```--cached``` option does nothing since the content is already gone. (git 1.9.2) – denis Jan 22 '15 at 14:12
  • 1
    @and-dev --cached removes from the index, not from the disk. – VonC Jan 22 '15 at 14:40
  • 4
    I found the deinit command put the subfolder back as an empty directory. The mv then put the temp subfolder into the new empty directory, rather than renaming it. So check the directories before doing the final mv. Otherwise, did the job. – Jason May 04 '17 at 16:32
  • just "git rm --cached subfolder" and "git add . " will help you – Tony Wang Sep 26 '20 at 11:43
  • @TonyWang Add what though? As commented, the `deinit` command put the subfolder back as an empty directory. – VonC Sep 26 '20 at 11:47
  • deinit is unnecessary when using only "git rm --cached subfolder" and "git add . ". The same thing happened to me, but I fixed it by the two cmds. – Tony Wang Sep 26 '20 at 11:55
  • @TonyWang It should be necessary, when the subfolder to remove is a submodule, as I documented in https://stackoverflow.com/a/16162000/6309. – VonC Sep 26 '20 at 12:02
15

First delete .git folder from the submodule.

Just running this removes the cached folder and contents from git but won't delete the folder.

git rm -r --cached [EnterFolderNameWithoutBrackets]
Joe Shakely
  • 623
  • 7
  • 9
  • 3
    Should be the accepted answer. I would add also delete .gitmodules and edit .git/config to remove the entries to the submodules, although I am not sure that is necessary. – Rafael Guimaraes Siqueira Feb 08 '22 at 14:16
  • Why should this be the accepted answer @RafaelGuimaraesSiqueira? Why is this better than the currently accepted one? – webejaxx Oct 25 '22 at 00:11
1

The answer from VonC will delete the submodule folder contents unless you rename the physical folder first - as shown in this answer, whose question was noted to be a duplicate by Chad:

https://stackoverflow.com/a/16162228

Community
  • 1
  • 1
fractos
  • 31
  • 3