%% date:: [[2021-02-12]], [[2021-01-27]], [[2020-12-09]], [[2023-05-06]] parent:: [[Git]] %% # [[Submodules in Git]] ## What is a submodule A submodule is the inclusion in one repository of a second repository, each of which can be modified independently of the other. ## Using a submodule ### When to use a submodule A submodule is used instead of a full clone of a needed library to allow for updates of that library or repo without manual intervention. ### Advantages of using submodules - It safeguards against accidental deployment, since deploying a change requires deliberately pushing it twice. - Having one project divided into two repos means there are more options for rolling back changes. - Increased branching capability allows for more experimentation, since the branch being used for one is not necessarily what's used for the other. ### Disadvantages of using submodules A disadvantage of the submodule approach is the lack of control; it increases the potential that the submodule repo, especially if it is one that is managed by a third party, will introduce changes that break the main repo's code or functionality. ![[How to Setup a Hugo Website on GitHub#^d1a5b0]] ## Working with submodules ### Add a submodule `git submodule add https://github.com/nicolevanderhoeven/reponame.git` ### Remove a submodule #### Remove the files associated to the submodule `rm -rf .git/modules/submodulename/` #### Remove any references to submodule in config `nano .git/config` Remove the part beginning with `[submodule "submodulename"]` #### Remove .gitmodules `rm -rf .gitmodules` #### Remove it from the cache without the "git" `git rm --cached submodulename` ### Push source project and submodule to remote repos together When working with one or more submodules in your project, push the committed changes in the submodule and then run the following command within the main project to push the new reference to the submodule along with the committed changes in the project repo together. `git push -u origin master --recurse-submodules=on-demand` This step is important because when the submodule is changed, the source project updates its reference. Pushing them together ensures that the reference is maintained. Or something like that. That's what I think happened when I didn't do it together. ## Related - [[Submodules vs subtrees vs worktrees in Git]]