# What is Git submodules?

<div><strong>Source</strong>: <a href='https://www.tuanh.net/blog/devops/what-is-git-submodules'>What is Git submodules?</a></div><div class="line-post"> 
 <h2 class="title-post">1. What Are Git Submodules?</h2> 
</div>
<div class="line-post">
  A Git submodule is a repository within another repository. Unlike simply copying code between projects, submodules allow you to embed an independent Git repository (the submodule) inside your main repository (the superproject) and manage it as if it were an integral part of the main project. This is especially useful when you have shared libraries or dependencies that must stay synchronized across multiple projects. 
</div>
<div class="images-post"> 
 <img width="80%" class="element_455787-image" src="https://resource.tuanh.net/article/image/0a35dde2-f5d5-48a4-ad8b-5985c52468e3.png" alt="Image"> 
</div>
<div class="line-post"> <strong>Key Characteristics of Git Submodules</strong> 
</div>
<div class="line-post"> 
 <ul> 
  <li><strong>Independence</strong>: Each submodule is an independent repository with its own commit history and branches.</li> 
  <li><strong>Version Control</strong>: The main repository points to a specific commit within the submodule, ensuring consistent versions across collaborators.</li> 
  <li><strong>Flexibility</strong>: You can update or freeze the version of the submodule, allowing precise control over dependencies.</li> 
 </ul> 
</div>
<div class="line-post"> 
 <h2 class="title-post">2. Setting Up Git Submodules</h2> 
</div>
<div class="line-post">
  Let’s walk through how to add, update, and manage a submodule within a Git repository. 
</div>
<div class="line-post"> 
 <h3 class="sub1">2.1 Adding a Git Submodule</h3> 
</div>
<div class="line-post">
  To add a submodule to your repository, you need the URL of the external repository. Use the following command within your main project: 
</div>
<pre class="code">git submodule add [URL] [path/to/submodule]</pre>
<div class="line-post">
  For instance: 
</div>
<pre class="code">git submodule add https://github.com/example/shared-library libs/shared-library</pre>
<div class="line-post">
  This command will: 
</div>
<div class="line-post"> 
 <ul> 
  <li>Clone the specified repository into the libs/shared-library directory of your main project.</li> 
  <li>Add an entry in the .gitmodules file to track the submodule's path and URL.</li> 
 </ul> 
</div>
<div class="line-post"> 
 <h3 class="sub1">2.2 Committing and Pushing Changes</h3> 
</div>
<div class="line-post">
  When you add a submodule, two files will be updated in your repository: .gitmodules and a directory entry for the submodule. Commit these changes to track the submodule’s presence: 
</div>
<pre class="code">git commit -am "Add shared-library submodule"<br>git push</pre>
<div class="line-post"> 
 <h3 class="sub1">2.3 Cloning a Repository with Submodules</h3> 
</div>
<div class="line-post">
  When a repository containing submodules is cloned, the submodules are not automatically cloned. To initialize and update the submodules after cloning, use: 
</div>
<pre class="code">git clone [URL to main repository]<br>cd [repository name]<br>git submodule update --init --recursive</pre>
<div class="line-post">
  This command fetches the code for each submodule and places it in the specified directory, syncing with the commit recorded in the main repository. 
</div>
<div class="line-post"> 
 <h2 class="title-post">3. Working with Submodules</h2> 
</div>
<div class="line-post">
  Managing submodules involves checking for updates, syncing, and occasionally handling conflicts. Below, we dive into these actions with practical examples. 
</div>
<div class="line-post"> 
 <h3 class="sub1">3.1 Updating a Submodule</h3> 
</div>
<div class="line-post">
  Sometimes, the repository within your submodule may be updated. To pull the latest changes into your submodule, navigate to the submodule’s directory and run: 
</div>
<pre class="code">git pull origin [branch]</pre>
<div class="line-post">
  After updating, go back to the main repository’s root, stage the new commit, and push the changes: 
</div>
<pre class="code">git add [submodule path]<br>git commit -m "Update submodule to latest version"<br>git push</pre>
<div class="line-post"> 
 <h3 class="sub1">3.2 Switching Branches within a Submodule</h3> 
</div>
<div class="line-post">
  You may need to work with different branches within the submodule. First, enter the submodule’s directory: 
</div>
<pre class="code">cd libs/shared-library<br>git checkout [branch]</pre>
<div class="line-post">
  After checking out a new branch or committing changes in the submodule, ensure you return to the main repository’s root and commit the updated submodule reference. 
</div>
<div class="line-post"> 
 <h3 class="sub1">3.3 Removing a Submodule</h3> 
</div>
<div class="line-post">
  Removing a submodule involves a few steps since Git tracks them in multiple places: 
</div>
<pre class="code">git submodule deinit -f [submodule path]<br>rm -rf .git/modules/[submodule path]<br>git rm -f [submodule path]</pre>
<div class="line-post"> 
 <h2 class="title-post">4. Best Practices for Git Submodules</h2> 
</div>
<div class="line-post">
  To avoid complications, adhere to these best practices when using Git submodules. 
</div>
<div class="line-post"> <strong>Use Submodules for Stable Dependencies</strong> 
</div>
<div class="line-post">
  Git submodules are ideal for dependencies that don’t change frequently. For volatile dependencies, consider alternatives like package managers, as submodules can become cumbersome to update and synchronize. 
</div>
<div class="line-post"> <strong> Regularly Sync with Your Team</strong> 
</div>
<div class="line-post">
  Ensure all team members run git submodule update --init --recursive after pulling changes in the main repository. This keeps everyone on the same page with the submodule versions. 
</div>
<div class="line-post"> <strong>Lock Submodule Versions</strong> 
</div>
<div class="line-post">
  When using submodules, avoid moving them to the latest commit every time. Locking them to specific commits ensures consistency, which is essential for code stability, especially in production environments. 
</div>
<div class="line-post"> 
 <h2 class="title-post">5. Troubleshooting Common Issues</h2> 
</div>
<div class="line-post">
  Git submodules can present unique challenges. Let’s look at some common issues and solutions. 
</div>
<div class="line-post"> 
 <h3 class="sub1">5.1 Detached HEAD State in Submodules</h3> 
</div>
<div class="line-post">
  Submodules often operate in a detached HEAD state, which can be confusing. To address this, you can explicitly check out a branch within the submodule. 
</div>
<pre class="code">cd [submodule path]<br>git checkout main</pre>
<div class="line-post"> 
 <h3 class="sub1">5.2 Recursive Cloning Issues</h3> 
</div>
<div class="line-post">
  For submodules that themselves contain submodules, add the --recursive option when cloning or updating: 
</div>
<pre class="code">git clone --recursive [repository URL]</pre>
<div class="line-post"> 
 <h2 class="title-post">6. Conclusion</h2> 
</div>
<div class="line-post">
  Git submodules offer a powerful yet complex way to manage code dependencies. By following these best practices and thoroughly understanding the submodule workflow, you can integrate external repositories into your projects effectively. However, always consider your project’s requirements before opting for submodules, as simpler solutions may be more appropriate for fast-changing dependencies. 
</div>
<div class="line-post">
  If you have any questions or need further clarification on working with Git submodules, feel free to leave a comment below. 
</div><p><strong>Read more at </strong>: <a href='https://www.tuanh.net/blog/devops/what-is-git-submodules'>What is Git submodules?</a></p>
