What is a Git Submodule, and why might you use a submodule instead of just copying the code into your repository?

Last updated 6 July 2026

  • A submodule is a Git repo nested in another, pinned to a specific commit: the parent stores the URL in .gitmodules plus a commit SHA (gitlink), not the files.

  • Unlike copying code, it keeps an upstream link and history so you can pull updates, push fixes, and pin exact versions reproducibly.

  • After cloning run git submodule update --init --recursive. Forgetting this and detached-HEAD confusion are common pitfalls (git subtree or a package manager are alternatives).

  • https://git-scm.com/book/en/v2/Git-Tools-Submodules
  • https://www.atlassian.com/git/tutorials/git-submodule
  • https://www.freecodecamp.org/news/how-to-use-git-submodules/
  • https://www.developernation.net/blog/how-git-submodules-can-save-you-time-and-headaches-taming-the-dependency-beast/
  • https://medium.com/@MakeComputerScienceGreatAgain/demystifying-git-submodules-a-guide-to-managing-external-dependencies-24e3b362c5f9
  • https://www.atlassian.com/blog/git/git-submodules
All Git interview questions