The main purpose of the git submodules is to allow for keeping a git repository as a subdirectory of any other git repository. In other words, git submodules are the reference for another repository. For example, suppose working on one project and using that project from within it. It means cloning that repository into our own repository, and it also provides a way to keep all the commits in a different way.
This blog goes into detail about submodules.
1. Introduction to submodules
2. Commands for submodules
3. Cloning with submodules
4. Updating submodules
Introduction to Submodules
Let's start with a simple project. First of all, we will discuss the add command. The main purpose of the add command is to add an already existing repository as a submodule of the repository that working on it. In other words, the add command with an absolute URL is used for adding a new submodule.
Default case: The submodules will be added to the subproject. If we want some specific location, then we can specify the path over there.
$ git submodule add https://github.com/sample
Commands for Submodules
There are many commands that will be used for submodules. Some important commands will be shown over here.
Git status
Status is among the most crucial and frequently utilized commands. This command serves its primary purpose by "revealing the state of both the working directory and the staging area." Its utility lies in identifying all modifications, although it does not extend to displaying any details about the committed project.
On branch staging
Your branch is ahead of 'origin/staging' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
Git diff
The other command is diff, it is mainly used to take two input data sets, and the changes between both of these two will be the corresponding output. To put it differently, "git diff" is a versatile Git command that, upon execution, initiates a differentiation process on Git data sources.
$ git diff --cached sample
Here, in the above-mentioned example, the sample will be a subdirectory in our main
Directory. The sample is a submodule, and it will never track the contents. Git will be
taken as a special commit from that repository.
Git commit
The git commit is another important command. It is mainly used for committing the
changes. Whenever pushing the latest content onto the repository, then all changes will be updated on the git repository.
git commit -m '[UPDATE] Command'
After committing we can see changes and it will be mentioned with the number of the files, and it also shows how many insertions and deletions are done.
[staging adc6y75] [UPDATE] "specified command"
1 file changed, 1 insertion(+), 1 deletion(-)
Here, one file is changed, and one insertion and deletion are done. Also updated with some specific commands. When committing, we can also mention changes related to the test here.
Cloning with submodules
Sometimes, the existing submodules should be updated. We have to update or modify the existing submodules in such cases to make sure that the system generates an SSH key.
Clone a submodule
After generating the SSH key, clone the submodule. Cloning can be done in two methods. The first method is using the following three commands. Before cloning, make a new local repository. After creating the local repository, through the terminal, we can clone the submodule. If it is not generated the ssh key, then it will generate an error. Hence, make sure that the SSH key is generated. To initiate the cloning process, the following command needs to be employed.
cybrosys@cybrosys:~/Desktop/Sample folder/jun 6$ git clone https://github.com/Sample/sample.git staging
Here in the above example, this command is mainly used for cloning the submodule in git. But here, the directory is empty. Before cloning a submodule, it should run two commands. The first command is
git submodule init
This command is mainly used for initialization purposes. Initializing our locally made file.
Hence, before cloning, make sure that the submodule should be initialized.
The main intention behind this command is to renew or update the data.
In other words, fetching all the data from the related project.
git submodule update
There is another method that is far easier than the previous one. Within this approach, only a single command needs to be utilized.
cybrosys@cybrosys:~/Desktop/Sample Folder/jun 6$ git clone --recurse-submodules https://github.com/Sample/Sample.git staging
Cloning into 'staging'...
Username for 'https://github.com': "username"
Password for 'https: "password"
In this method, clone the submodule using simple commands instead of using three commands. It will be a combination of the three commands. After the cloning command, you have to specify the username and then the password.
Updating Git Submodules
There are many existing submodules. In some cases, we have to update these submodules. Here, we discuss all the commands step by step. Initially, generate the ssh key. Only after that updating submodules will be possible. After that, we have to make a local repository and clone the submodule. As mentioned earlier, use any of the methods and clone the submodule.
cybrosys@cybrosys:~/Desktop/Sample Folder/jun 6$ git clone --recurse-submodules https://github.com/Sample/Sample.git staging
Cloning into 'staging'...
Username for 'https://github.com': "username"
Password for 'https: "password"
After cloning the submodule, specify the related username and password.
Cloning into 'staging'...
Username for 'https://github.com': “username”
Password for 'https://username@github.com':”password”
remote: Enumerating objects: 1015, done.
remote: Counting objects: 100% (47/47), done.
remote: Compressing objects: 100% (28/28), done.
remote: Total 1015 (delta 14), reused 37 (delta 4), pack-reused 968
Receiving objects: 100% (1015/1015), 2.27 MiB | 1.64 MiB/s, done.
Resolving deltas: 100% (323/323), done.
Submodule 'sample' (git@github.com:smaple. git) registered for path 'sample'
Submodule 'OCA/server tools (git@github.com: OCA/server-tools.git) registered for path 'OCA/server tools
Submodule 'OCA/social' (git@github.com: OCA/social.git) registered for path 'OCA/social'
Submodule 'OCA/web' (git@github.com: OCA/web.git) registered for path 'OCA/web'
Cloning into '/home/cybrosys/Desktop/Sample Folder/jun 6/staging/sample'...
remote: Enumerating objects: 5692, done.
remote: Counting objects: 100% (1639/1639), done.
remote: Compressing objects: 100% (560/560), done.
remote: Total 5692 (delta 1112), reused 1382 (delta 1040), pack-reused 4053
Receiving objects: 100% (5692/5692), 4.04 MiB | 1.34 MiB/s, done.
Resolving deltas: 100% (3789/3789), done.
Cloning into '/home/cybrosys/Desktop/Sample Folder/jun 6/staging/OCA/server-tools'...
remote: Enumerating objects: 100379, done.
remote: Counting objects: 100% (1951/1951), done.
remote: Compressing objects: 100% (806/806), done.
remote: Total 100379 (delta 1186), reused 1803 (delta 1114), pack-reused 98428
Receiving objects: 100% (100379/100379), 44.62 MiB | 7.01 MiB/s, done.
Resolving deltas: 100% (65671/65671), done.
Cloning into '/home/cybrosys/Desktop/Sample Folder/jun 6/staging/OCA/social'...
remote: Enumerating objects: 46566, done.
remote: Counting objects: 100% (5131/5131), done.
remote: Compressing objects: 100% (774/774), done.
remote: Total 46566 (delta 4766), reused 4397 (delta 4353), pack-reused 41435
Receiving objects: 100% (46566/46566), 10.66 MiB | 1.84 MiB/s, done.
Resolving deltas: 100% (32878/32878), done.
Cloning into '/home/cybrosys/Desktop/Sample Folder/jun 6/staging/OCA/web'...
remote: Enumerating objects: 62034, done.
remote: Counting objects: 100% (788/788), done.
remote: Compressing objects: 100% (351/351), done.
remote: Total 62034 (delta 428), reused 730 (delta 418), pack-reused 61246
Receiving objects: 100% (62034/62034), 106.90 MiB | 5.29 MiB/s, done.
Resolving deltas: 100% (27851/27851), done.
* Next, back to our folder of the odoo.sh-linked Repository, and after that Commit it and Push the latest code.
This is how we can update the submodules. This makes it very easy to make updates to existing submodules.
To read more about creating and configuring sub-modules on the Odoo.sh?, refer to our blog How to Create and Configure Sub-modules on the Odoo.sh?