Adicionando e Clonando Submódulos
Aprenda os comandos para adicionar um novo submódulo a um projeto e clonar corretamente um repositório com submódulos.
Adicionando e Clonando Submódulos é uma aula grátis de Git Advanced: Monorepo, Submodules & Workflows no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Git Advanced: Monorepo, Submodules & Workflows, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Git Advanced: Monorepo, Submodules & Workflows inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
Add & Clone Git Submodules
Git submodules let you embed one Git repository inside another. This is useful for managing external dependencies like libraries or shared components.
In this lesson, we'll learn how to add a new submodule to your project and how to properly clone a repository that contains submodules.
The `git submodule add` Command
To add a submodule, you use the git submodule add command. This command takes the URL of the external repository and optionally a local path where you want to place it.
- Repository URL: The URL (HTTPS or SSH) of the submodule's Git repository.
- Path: The directory within your main project where the submodule will reside. If omitted, Git uses the repository name.
Add Submodule in Action
Let's say you have a my-project repository and want to include a shared library from https://github.com/example/shared-lib.git.
First, navigate to your main project's directory.
cd my-project
git submodule add https://github.com/example/shared-lib.git lib/shared-libFiles Created by `submodule add`
After running git submodule add, Git does a few things:
- It clones the submodule repository into the specified path (e.g.,
lib/shared-lib). - It adds an entry to a special file called
.gitmodulesin your main repository's root. - It stages the new submodule directory and the
.gitmodulesfile for commit.
Inspecting `.gitmodules`
The .gitmodules file is a configuration file that stores the mapping between the submodule's local path and its remote URL. It's crucial for others to clone your project correctly.
Here's what it might look like for our example:
[submodule "lib/shared-lib"]
path = lib/shared-lib
url = https://github.com/example/shared-lib.gitCommit Your Submodule Change
Adding a submodule is a change to your main repository, so you need to commit it. The commit will record the specific commit hash of the submodule's repository that your main project is currently using.
git status
git commit -m "Add shared-lib as a submodule"Cloning a Parent Repository
When you clone a repository that contains submodules, Git clones the parent repository but doesn't automatically download the submodule content.
The submodule directories will appear empty or contain only a .git file pointing to the submodule's repository.
The Easy Way to Clone All
The most straightforward way to clone a repository and all its submodules is to use the --recurse-submodules flag with git clone.
This command clones the main repository and then automatically initializes and updates all submodules to their correct versions.
git clone --recurse-submodules https://github.com/your/my-project.gitManually Initializing Submodules
If you forget --recurse-submodules during cloning, don't worry! You can initialize and update them manually after cloning the parent repository.
First, navigate into the cloned parent repository.
cd my-project
git submodule initFetching Submodule Content
After initializing, you need to tell Git to fetch the actual content for each submodule. This is done with the git submodule update command.
It fetches the data from the submodule's remote and checks out the specific commit recorded by the parent repository.
cd my-project
git submodule updateCheck Your Submodule Knowledge
You've just cloned a repository named parent-repo which is known to contain a submodule. After cloning with just git clone parent-repo.git, what are the necessary commands to get the submodule's content ready for use?
Recap: Add & Clone Submodules
In this lesson, you learned how to:
- Add a submodule using
git submodule add <url> <path>, which creates a.gitmodulesfile and stages the submodule. - Commit submodule changes to record the specific version of the submodule in the parent repository.
- Clone a repository with submodules using
git clone --recurse-submodulesfor an all-in-one operation. - Manually initialize and update submodules with
git submodule initandgit submodule updateif you cloned without the recurse flag.
Next, we'll explore how to manage and update submodules after they've been added and cloned.
Perguntas Frequentes
A aula “Adicionando e Clonando Submódulos” é grátis?
Sim — o texto completo de “Adicionando e Clonando Submódulos” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Git Advanced: Monorepo, Submodules & Workflows, atualize para CoddyKit PRO. O curso de Git Advanced: Monorepo, Submodules & Workflows inclui 4 aulas no total.
O que vou aprender em “Adicionando e Clonando Submódulos”?
Aprenda os comandos para adicionar um novo submódulo a um projeto e clonar corretamente um repositório com submódulos. Você pratica Git Advanced: Monorepo, Submodules & Workflows com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Git Advanced: Monorepo, Submodules & Workflows?
Nenhuma experiência prévia é necessária. Git Advanced: Monorepo, Submodules & Workflows no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.
Quanto tempo leva a aula “Adicionando e Clonando Submódulos”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Git Advanced: Monorepo, Submodules & Workflows?
Sim. Cada aula de Git Advanced: Monorepo, Submodules & Workflows inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Introdução aos Submódulos do Git
- Adicionando e Clonando Submódulos
- Atualizando e Sincronizando Submódulos
- Removendo e desinicializando submódulos