Issue with adding common code as git submodule: "already exists in the index"
I'm afraid there's not enough information in your question to be certain about what's going on, since you haven't replied to my follow-up question, but this may be of help in any case.
That error means that projectfolder
is already staged ("already exists in the index"). To find out what's going on here, try to list everything in the index under that folder with:
git ls-files --stage projectfolder
The first column of that output will tell you what type of object is in the index at projectfolder
. (These look like Unix filemodes, but have special meanings in git.)
I suspect that you will see something like:
160000 d00cf29f23627fc54eb992dde6a79112677cd86c 0 projectfolder
(i.e. a line beginning with 160000
), in which case the repository in projectfolder
has already been added as a "gitlink". If it doesn't appear in the output of git submodule
, and you want to re-add it as a submodule, you can do:
git rm --cached projectfolder
... to unstage it, and then:
git submodule add url_to_repo projectfolder
... to add the repository as a submodule.
However, it's also possible that you will see many blobs listed (with file modes 100644
and 100755
), which would suggest to me that you didn't properly unstage the files in projectfolder
before copying the new repository into place. If that's the case, you can do the following to unstage all of those files:
git rm -r --cached projectfolder
... and then add the submodule with:
git submodule add url_to_repo projectfolder
You need to remove your submodule git repository (projectfolder in this case) first for git path.
rm -rf projectfolder
git rm -r projectfolder
and then add submodule
git submodule add <git_submodule_repository> projectfolder
Removing submodule manually involves number of steps and this worked for me.
Assuming you are in the project root directory and sample git module name is "c3-pro-ios-framework"
Remove the files associated to the submodule
rm -rf .git/modules/c3-pro-ios-framework/
Remove any references to submodule in config
vim .git/config
Remove .gitmodules
rm -rf .gitmodules
Remove it from the cache without the "git"
git rm --cached c3-pro-ios-framework
Add submodule
git submodule add https://github.com/chb/c3-pro-ios-framework.git
I had the same problem and after hours of looking found the answer.
The error I was getting was a little different: <path> already exists and is not a valid git repo
(and added here for SEO value)
The solution is to NOT create the directory that will house the submodule. The directory will be created as part of the git submodule add
command.
Also, the argument is expected to be relative to the parent-repo root, not your working directory, so watch out for that.
Solution for the example above:
- It IS okay to have your parent repo already cloned.
- Make sure the
common_code
directory does not exist. cd Repo
-
git submodule add git://url_to_repo projectfolder/common_code/
(Note the required trailing slash.) - Sanity restored.
I hope this helps someone, as there is very little information to be found elsewhere about this.
if there exists a folder named x
under git control, you want add a same name submodule , you should delete folder x
and commit it first.
Updated by @ujjwal-singh:
Committing is not needed, staging suffices.. git add
/ git rm -r