A submodule is a record within a host git repository that points to a specific commit in another external git repository. Most important concept revolving that why to use submodule in git. This is used in a peculiar case where we wanted not only to share our code but at the same time want the consumer of the code to make changes in the same code.
Adding Git submodules
Adding submodules in git
Here, we can see to add a submodule in the git repository we have used the command:-
git submodule add url_of_the_git_repository
The above command takes URL as a parameter that points to a repository on Github. Here we have added the
Submodules-in-git as a submodule. This command will clone the submodule and after that, we can check the status of the git repository using the git status command.
Here we can see that using the below command git status as it shows us the two files that are .gitmodules and the submodules-in-git directory. Now let’s see the contents of the .gitmodules file. These commands are as depicted via terminal/ PowerShell in below media as follows:
Contents of .gitmodules
Cloning a GitHub repository having .gitmodules
For Cloning the repository having submodules we have to use the following command:
git clone ---recursive url_of_the_github_repository
Cloning GitHub repository having submodules
It is used to copy the mappings from a .gitmodules file to a ./.gitconfig file. Moreover, it is very helpful when there are many
submodules present in a repository activate only specific submodules which are needed for working in the repository.
Using git submodule init
Workflows in submodules
As soon as you initialize the submodules and update them within a parent repository now we can use them as a different repository that can have their own branches and history. So when we are creating some changes in the submodule it’s important to have a track on them and commit them properly. So let’s switch to our submodule here Submodules-in-git
Adding and Committing files in the submodule
Now let’s move back to the parent repository of this submodule which is present in folder10 and let’s see the status of the parent repository. As we can see that now the parent repository is aware that some changes are made in the submodules Now if one wants to update this parent repository you can do so by using git add and git commit commands.
Using git status command in the parent repository
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!