Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • jQuery
  • AngularJS
  • ReactJS
  • Next.js
  • React Native
  • NodeJS
  • Express.js
  • MongoDB
  • MERN Stack
  • PHP
  • WordPress
  • Bootstrap
  • Tailwind
  • CSS Frameworks
  • JS Frameworks
  • Web Development
Open In App
Next Article:
Submodules in Git
Next article icon

Submodules in Git

Last Updated : 14 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In software development, it’s common to manage projects that depend on other projects. Git submodules are a powerful feature that allows you to include and manage external repositories within your main repository. This feature enables you to keep multiple repositories in sync and makes it easier to work on complex projects that rely on various codebases. This article will guide you through the concept of Git submodules, their benefits, and how to use them effectively.

What Are Git Submodules?

Git submodules are pointers to another repository at a specific commit. They allow you to include a separate repository within your main repository, maintaining a connection between the two. This means you can manage dependencies as part of your project without merging all the code into a single repository.

Benefits of Using Git Submodules

  • Modularity: Keep your project modular by separating dependencies into distinct repositories.
  • Isolation: Manage changes in external dependencies without directly affecting your main project.
  • Version Control: Pin dependencies to specific versions, ensuring stability and consistency.
  • Reusability: Reuse submodules across multiple projects, avoiding code duplication.

Use Cases for Git Submodules

  • Large Projects: Split large projects into manageable components.
  • Shared Libraries: Include common libraries shared across different projects.
  • Third-Party Dependencies: Manage third-party code dependencies separately from your main codebase.

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

Git init

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



Next Article
Submodules in Git

A

ankitmahajan852
Improve
Article Tags :
  • Git
  • Web Technologies

Similar Reads

    Use of Submodules in GitHub
    Submodules in GitHub provide a way to incorporate one Git repository into another as a subdirectory. This feature is especially useful for managing dependencies, reusing code across projects, and maintaining modular codebases. In this article, we will explore the concept of Git submodules, their ben
    4 min read
    How to Add Submodule in Git?
    Git is a widely used distributed version control and source code management system. It effectively tracks changes to source code, enabling easy branching, merging, and versioning. In this article, we will explore the concept of Git submodules, their benefits, and best practices for integrating them
    4 min read
    Updating a Submodule in Git
    Git submodules are a powerful feature that allows you to include and manage external repositories within your own Git project. This is particularly useful for managing dependencies or handling third-party libraries. However, keeping these submodules up to date can be a bit tricky. In this article, w
    3 min read
    Git Subtree vs. Git Submodule
    Git Subtree and Git Submodule are both mechanisms in Git that allow you to incorporate external repositories into your own repository. They provide a way to manage dependencies and include code from other projects while keeping your repository organized and maintainable. Git Subtree: Git Subtree all
    4 min read
    Git - Index
    In Git, the index, also known as the staging area, plays an important role in the version control process. It acts as an intermediary between your working directory and the repository, allowing you to prepare and review changes before committing them. This article explores the concept of the Git ind
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences