Sometimes, a project needs to use another Git repository, perhaps a third-party repository, or a separate project. How do all Git repositories coexist in one project directory? Git subModule comes in handy.

What is Git subModule

Git submodules are Git submodules that allow you to use one Git repository as a subdirectory of another Git repository. Same project directory, but separate Git recording and commit.

How to create a Git SubModule

Git submodule add < submodule git address >

Git 1.8.2+

Git submodule add -b < submodule branch > < submodule git address >

Then git add. && Git commit saves the record.

Run tree -a to view the structure changes of the current repoA directory

. ├ ─ ─. Git │ ├ ─ ─ HEAD │ ├ ─ ─ branches │ ├ ─ ─ the config │ ├ ─ ─... │ ├ ─ ─ modules # git record new modules │ │ └ ─ ─ the plugins │ │ └ ─ ─ repoB │ │ ├ ─ ─ HEAD │ │ ├ ─ ─ branches │ │ ├ ─ ─ the config │ │ ├ ─ ─... │ ├── ├─ ├─ ├─ ├─ │ ├── ├── │ ├── ├── ├── ─ README.mdCopy the code

How do I clone a project with submodules

If cloned for the first time, use--recurse-submodulesparameter

Git clone --recurse-submodules

Clone the submodules in the cloned project

  1. Normal clone warehouseGit clone <git repository >
  2. The current project directory executesgit submodule initTo initialize the local configuration file
  3. performgit submodule updateCheck all updates to the project submodule

Git submodule update –init –recursive

How to update Git SubModule

The main repository needs to be updated after the submodule is modified

The update of the main repository and submodule is consistent with the normal use of Git projects. The main repository still needs to submit the updated submodule to Git records after the module changes.

Note that the git subModule only points to a COMMIT SHA value. You need to checkout to the branch you want to commit to.

You then need to commit the changes to the main warehouse again

Submodules have updates synchronized locally

Git pull directly for the main project (but note that the subModule is on a modified branch).

How to delete Git SubModule completely

Quite complex as follows:

  1. Git rm –cache

  2. Rm -rf <git submodudle >

  3. Delete. Gitmodules or edit. Gitmodules to delete records of the submodule in the file

       [submodule "plugins/repoB"]
       path = plugins/repoB
       url = [email protected]:paddingme/repoB.git
       branch = dev
    Copy the code
  4. Modify submodule contents in./git/config

    [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true ignorecase = true precomposeunicode = true [remote "origin"] url = [email protected]:paddingme/repoA.git fetch = +refs/heads/*:refs/remotes/origin/* [branch "main"] remote = origin merge = refs/heads/main [submodule "plugins/repoB"] # delete url = [email protected] here: paddingme/repoB git # # delete active = true to deleteCopy the code
  5. Delete. Git related sub module file in folder If the rm – rf. Git/modules/plugins/repoB /

  6. git add .&& commit

  7. Git submodule sync is the best option.

I’m not going to do that. I’m tired.

conclusion

As mentioned above, git submodules are a bit complicated to use and have some learning costs. Remember the following two things when using Git submodules, and come back to this article more often.

  1. The commonly usedgit statusView the current directory status
  2. Verify that the SubModule is on the correct branch

References

  • 7.11 Git Tools – Sub-modules

Padding. me/git-submodu…