简体   繁体   中英

Can I use git submodule within the same repository?

In my project there is a folder with common tools that I want to include in each of my app components. I just want one master version of it and I want the rest of the copies to be updated with git commands as normal. Is there a way to do this while keeping everything in the same repository? I know got submodule works with multiple repositories but it doesn't seem like it works with just a single repository. Does anyone have any insight on the matter?

In my project there is a folder with common tools that I want to include in each of my app components. I just want one master version of it and I want the rest of the copies to be updated with git commands as normal. Is there a way to do this while keeping everything in the same repository? I know got submodule works with multiple repositories but it doesn't seem like it works with just a single repository. Does anyone have any insight on the matter?

Experimenting a little, the answer seems to be "sort-of". The command supports it with something like

git submodule add -b libfoo -- ./ libs/foo

where libfoo is the name of the branch that the submodule exists in, ./ is a relative URL to the "upstream repository", and libs/foo is the "working tree" of the submodule checkout (and also the default name of the submodule; you may want to override this with --name libfoo ).

Digging around a little, it doesn't quite do what you might want:

  • It seems to do a ~full checkout (with all the remote tracking branches, not just the submodule branch).
    • You can probably fix this by editing .git/modules/libs/foo/config .
  • It does not share storage with the "enclosing" repo.
    • You can probably make it share storage with symlinks (as git-new-workdir does).
    • I'm less sure about making this share storage in the same way as git worktree . Maybe this is just a matter of pointing .git/modules/libs/foo/commondir at the enclosing repo?
  • If you have an origin (aka upstream?) set, I think the branch must already exist on the origin repo.
  • If you don't have an origin set, it uses the absolute path of the local repo as the origin "URL" of the submodule (see .git/modules/libs/foo/config ). This will break if you move your checkout somewhere else on the filesystem.

git-subtrac (from the author of git-subtree) is designed around the "submodules in the same repo" idea, but I'm not sure how much it helps with the above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM