In Git, developers want to use one repository within another repository. For that purpose, there is a need to make a submodule or sub-repository inside the Git local repository. A Git submodule is a method for including one Git local directory as a subdirectory within another Git repository at a specific path. Submodules can be utilized for keeping external dependencies separate from the main codebase and provide the best and easy way to manage complex project structures.
This tutorial will demonstrate the method for adding the submodule directory in Git.
How to Add a Submodule in Git?
To add a submodule in Git, follow the below-stated procedure:
-
- Go to Git local directory.
- Make a submodule inside the Git local repository.
- Move toward the submodule directory.
- Initialize the submodule directory.
- Next, log in to your GitHub account and navigate to “Your repositories> Select a repository> Copy HTTPS” URL.
- Insert the submodule using the “git submodule add” command along with the copied URL.
- Verify the added module by checking the Git status.
- Commit the tracked module using the “git commit” command.
Step 1: Redirect to the Git Local Repository
First, open the Git utility and navigate toward the Git local repository using the “cd” command:
Step 2: Make a SubModule Directory
Execute the “mkdir” command to make a subdirectory inside the current directory:
Step 3: Go to Submodule Directory
Navigate to the submodule directory using the “cd” command:
Step 4: Initialize Submodule Directory
Next, initialize the created submodule directory by executing the “git init” command:
As a result, an empty repository has been initialized successfully:
Step 5: Copy HTTPS URL
Next, log in to your GitHub account and navigate to “Your repositories> Select a repository> Copy HTTPS” URL:
Step 6: Add Submodule
Run the “git submodule add” command and paste the copied URL:
The resultant image indicates that the submodule repository has been cloned with the stated remote repository successfully:
Step 7: Check the Status of Git
Now, view the current status by running the “git status” command:
It can be noticed that the new changes have been added in the staging area:
Step 8: Commit Changes
Now, commit all the changes by executing the “git commit” command with the “-m” flag for committing the message:
The below output shows that all the modifications have been committed:
That’s all about adding the submodule in the Git local directory.
Conclusion
To add a submodule in git, first, move to the Git local directory and create a submodule inside the Git local repository using the “mkdir” command. Then, move to the submodule directory and initialize it. Next, log in to your GitHub account and navigate to “Your repositories> Select a repository> Copy HTTPS URL”. Insert the submodule with the “git submodule add” command along the copied link. Lastly, commit the added modification with the help of the “git commit” command. This post has stated the method for adding the submodule in Git.