Git Submodule- An Overview

Reading Time: 3 minutes

1 – What is a Git Submodule?

Actually git provides the feature of git submodule to help projects with this .Sub modules provide the feature of embedding one repository into another as a sub folder.

In simple words git allows you to include other repositories into a single repository called parent repository. Now this helps in tracking the changes in all the repositories from a central repository.

1.1 -Where is the submodule located ?

You can find the submodule anywhere inside the parents repository working directory.We can configure the submodule via .gitmodules file which contains the path of all submodules and the link to clone them.

2-How to create repository with submodules ?

2.1 To add a submodule in a git repository.

To add a submodule use ‘git submodule add’ and specify the url of the repository you want to add as a submodule.

$ git submodule add <remote_url> <destination_folder>

While adding your submodule you will find that it will be staged. So as a consequence you will need to commit it.

The command to commit it is :-

$ git commit -m "Added the submodule to the project."
$ git push

This example will show you in a better way.

git submodule add https://github.com/project/project.git assign
Cloning into '/home/rishivant/Documents/ut it assignment/assign'...
Username for 'https://github.com': rishvantsingh
Password for 'https://rishvantsingh@github.com':
Cloning into '/home/rishivant/main/project'...
remote: Enumerating objects: 3025, done.
remote: Total 3025 (delta 0), reused 0 (delta 0), pack-reused 5257
Receiving objects: 100% (5257/5257), 2.03 MiB | 2.38 MiB/s, done.
Resolving deltas: 100% (2139/2139), done.

So the above example shows adding a project submodule in my project in a folder named assign.

3-Submodules Update and Tracking Commits

There is always an option to use the best commit. The parent repository tracks all the commits.

A submodule update checks out the revision of all the commits that the parent repository has gone through after a pull.This is useful when you want to roll back to the current commit of the parent repository. This is usually common when you work on several checked out branches and you want to roll back to the commit tracked by parent repository.

4-To Pull a Git Submodule

Working as a developer sometimes you need to pull a submodule on another project. So lets do it

To pull a submodule with its content you the below command

$ git submodule update --init --recursive

THe below example shows how it works

$ git submodule update --init --recursive

Submodule 'asign' (https://github.com/project/project.git) registered for path 'assign'
Cloning into '/home/parth/submodules/assign'...
Submodule path 'assign': checked out '26d08138766p79574569c9d4cbeea9c7390039137'

So we can see the working on the same project as in previous example.

If the whole command is not used the folder will be pulled but not the content.

5-Updating a Git Submodule

In some cases you dont want to pull a submodule. So you can update the previous submodule in the project.

The below command

$ git submodule update --remote --merge

The -remote helps to update the existing repository without pulling it.

6- Deleting a Submodule

To delete a submodule one can use the below commands. Suppose my submodule name is <rishimodule> then run the below command like

git submodule deinit -f — rishimodule

rm -rf .git/modules/rishimodule

git rm -f rishimodule


Conclusion

In this article we learned what a git submodule is and how we can use it while working with different projects. Gitmodule has made the workflow really easier to work with different projects at a time. It has become a suitable way to present the parent and child workflow in the forom of git submodules.

To read more about Git you can check out at – https://blog.knoldus.com/introduction-to-git-flow/

Reference

https://www.vogella.com/tutorials/GitSubmodules/article.html

Leave a Reply