Use of Submodules in GitHub

01 Oct, 2020
Github is a web-hosting service for your Git repository. In Github, we can Store our Repository to store our source code and Collaborate with different users that’s why it is known as an open-source tool. In the event that you get what Git is and the basics of how it functions, at that point utilizing Git adequately will likely be a lot simpler for you. As you learn Git, attempt to free your psyche from the things you may think about different VCSs, such as CVS, Subversion, or Perforce — doing so will assist you with keeping away from unobtrusive disarray when utilizing the apparatus. Despite the fact that Git’s UI is genuinely like this different VCSs, Git stores and contemplates data in a totally different manner, and understanding these distinctions will assist you with keeping away from getting befuddled while utilizing it.
The subdirectory in a directory or Github repo inside another repository like using another project from within it is termed as a Submodule.
Like when you are working in a repo and you want it to use at one of your parent repositories.  You only add information about the submodule that is added to the main repository. This information describes which commit the submodule is pointing at. Now if you update the submodules it will not affect your parent repository as submodule when added will be pointing to that commit only when you create that submodule.
So, Submodules allow you to keep a Git repository as a subdirectory of another Git repository. This lets you clone another repository into your project and keep your commits separate. Also, updating that submodule repository will not update the submodule used in the parent repository.
As Submodules are generated according to our needs if submodules get updated commit by commit you will see the unexpected behavior.
Step 1: Use the following command: 
git submodule add  https://github.com/kushagra67414/a3.git
Step 2: Run Command: 
git diff –-cached –-submodule
Note: You will notice that Git sees it as a submodule and doesn’t track its contents when you’re not in that directory. Also, it counts it as a commit of your present repository.
Step 3: Use Command to add changes to the staging area:
git add  (add all changes file to staging area)  
or
Git add <filename>  (to add a specific file at staging area)
Step 4:
Git commit –m “comment” [create a snapshot at central ]
Note: 16000 in this image implies that you’re recording a commit as a directory entry rather than a subdirectory or a file
Step 5: Push changes to the repository
git push ( to push changes locally to remotely )
Note: If we clone a repo having submodules in it, then it will clone submodules files/data too because git counts it as a commit of the present repository.
Submodules are not automatically updated we need to fetch the data first and initialize our local configurations file.
We Add a file in repository a2 on Github. Now a2 is a submodule at repo a1  
Step 1: Go to repo a1 and then at submodule at a2
Run commands: 
cd a1 and cd a2
Now we are under a2 repo [a submodule of repo a1 ]
Step 2: Pull the changes done remotely
git pull
Step 3: Now the file will come at locally at your submodule folder a2
Remember one thing file is fetch at submodule a2 and changes at a1 repo have to be done. Because when directed to the submodule it acts as a repository. Now redirect to the parent repository.
Step 4: Check the status of repo a1.
git status
You will see changes made in your a1 repo are unstaged, stage it and commit it to have a snapshot of a repo
Step 5: git add .  [ . means staging all the files n folder present in the repository]
git commit –m “comment”
Step 6: Use the command git push  
Refresh your GitHub you will see the changes  
Sometimes when we clone the project with submodules, the directories that contain submodules, but none of the files within them.
The submodules can be or can not be empty. If it is empty fetch the data use the below command:
Method 1:
Open your repo at git bash and redirect to your local repository in bash then:
Step 1: Use command git submodule init  
This is used to initialize your local configuration file,
Step 2: git submodule update
Its function is to fetch the data from the project
Method 2:
Instead of using the above method, we can use a single command
git clone –recurse-submodules  https://github.com/kushagra67414/a1.git
Advantage of this method is that it fetches and updates submodules inside the submodule i.e nested submodule
GitHub
Git
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.