How to `git submodule add` Existing sub Repository?

后端 未结 5 1900
既然无缘
既然无缘 2021-01-18 02:56

The Question

How to add existing sub repository as a submodule in git?

The Why

I have a private codespace supermodule with submodule

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-18 03:26

    Where you went wrong is doing the

    $ git add .
    

    That adds everything, so also foo/bar, to the index of the current repository (ready to be committed thus).

    If you just don't do that and continue with

    $ git submodule add https://github.com/CarloWood/XYZ.git foo/bar
    

    then that should work; this would detect that foo/bar is an already cloned repository and add it to the current repository as a submodule.

    Note that it is not needed to clone first. You explicitly say you already have done that, but for clarity for other readers I'd like to point out that if you omit the clone right before the git add . too (so there isn't a foo/bar at all now) then the above git submodule add ... would see there isn't anything yet and then simply clone it for you.

    Note that there is a minor difference between methods. If you start with cloning then foo/.git will be a directory, while if you use git submodule add to do the cloning then this .git repository is put in .git/modules/foo of the parent project and foo/.git is a file containing the path to that. There is no real difference however as using a file for the .git to point anywhere else is generic and could be used anywhere; you can not conclude anything from .git being a file or directory.

提交回复
热议问题