Using the git submodule command to interact with other remote repositories
git submodule command, you can interact with submodules in your repository. A submodule allows you to specify a subdirectory in your repository that's actually another Git repository. This is most useful when you want to re-use the same code (such as a developer library) across different projects.
To add a submodule, you just need to run the following command from the root of your repository:
$ git submodule add https://githuh.com/myotherproject/myotherrepository subfolder Cloning into 'subfolder'... remote: Counting objects: 11, done. remote: Compressing objects: 100% (10/10), done. remote: Total 11 (delta 0), reused 11 (delta 0) Unpacking objects: 100% (11/11), done. Checking connectivity... done.
By default, Git will add a new folder directly in the path you run the command from named as the same name as the repository you're referencing. You can override this as we have done above, by adding a specific folder name after the remote repository URL.
This will create (if it doesn't already exist) a
.gitmodules file with 3 lines, referencing the local path that contains the remote repository's contents, as well as the URL for it.
Once you've added the submodule, you'll need to commit the change and push it to your remote:
$ git commit -am "Added myotherrepository submodule" [master 1234abc] Added myotherrepository submodule 2 files changed, 4 insertions(+) create mode 100644 .gitmodules create mode 160000 subfolder $ git push origin master
You might wish to update a referenced submodule if the remote repository itself has been updated.
$ git submodule update --remote --merge
This will update each submodule in your repository to the latest commit in their referenced remote repository. You'll then need to commit that change and push it to the remote repository.
If you were previously using a submodule and wish to remove it from your project, simply run:
$ git rm --cached /repo/submodule
Then commit your change.