Introdução ao Git Subtree
Entenda o que é o Git Subtree, como ele difere dos submódulos e quais são seus principais benefícios para a integração de projetos.
Introdução ao Git Subtree é uma aula grátis de Git Advanced: Monorepo, Submodules & Workflows no CoddyKit. Esta é a aula 1 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.
Discover Git Subtree
Welcome! In this lesson, we'll explore Git Subtree. It's a way to embed another Git repository inside your own, essentially merging its history and files into a subdirectory.
Think of it as an alternative to Git Submodules for managing external dependencies or shared code within a single project.
Subtree vs. Submodule: Key Difference
The core difference between Subtree and Submodule lies in how they integrate external code:
- Submodules: Store a reference (a specific commit hash) to an external repository. The external code lives separately, managed as a distinct repository.
- Subtree: Integrates the external repository's code and its history directly into your main repository. It's like a deep copy with the ability to link back to the original.
How Subtree Works Internally
When you add a subtree, Git performs a specialized merge operation. It takes the history of the external repository and 'rewrites' it to appear as if it always existed within a specific subdirectory of your main project.
This means you can treat the subtree's files just like any other files in your repository, using standard Git commands.
Benefit 1: Simplified Workflow
One major advantage of Git Subtree is its simplicity. There are no special .gitmodules files to track, nor do you need to initialize or update submodules after cloning.
Your entire project, including the subtree's content, is managed with standard Git commands like git clone and git pull.
Benefit 2: Everything in One Repo
With Git Subtree, all your code lives in a single repository. This can simplify several aspects of development:
- Cloning: A single
git clonecommand fetches everything. - Branching: Changes across your main project and the subtree can be made on one branch.
- CI/CD: Builds and tests typically don't need to fetch separate repositories.
Benefit 3: Smoother Collaboration
Many developers find subtrees easier to work with. Collaborators don't need to learn new Git commands specific to submodules or deal with their unique quirks.
The external code is simply part of the main repository, reducing potential friction when setting up development environments or fetching updates.
Adding a Subtree: The Command
To add a new external project as a subtree, you use the git subtree add command. You'll specify:
- The prefix (the subdirectory where it will live).
- The URL of the external repository.
- The branch you want to pull from (e.g.,
mainormaster).
Example: Adding a Subtree
Let's say you want to add a common utility library from GitHub into a vendor/utils directory in your project:
git subtree add --prefix=vendor/utils \
https://github.com/myorg/common-utils.git main \
--squashThe `--squash` Option Explained
Notice the --squash option in the previous example. This is very useful when adding a subtree!
It takes all the commits from the external repository's history and condenses them into a single commit when adding them to your main project. This prevents your main repository's history from being cluttered with potentially hundreds of commits from the external project.
Quick Check
Git Subtree offers distinct advantages over submodules in certain scenarios. Let's test your understanding.
Recap: Git Subtree Basics
Great job! In this lesson, we introduced Git Subtree, a powerful alternative to submodules for integrating external projects. You learned that subtrees merge external code directly into your repository, simplifying workflows and keeping all code in one place.
We also covered the git subtree add command and the benefit of the --squash option for cleaner history.
Perguntas Frequentes
A aula “Introdução ao Git Subtree” é grátis?
Sim — o texto completo de “Introdução ao Git Subtree” é 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 “Introdução ao Git Subtree”?
Entenda o que é o Git Subtree, como ele difere dos submódulos e quais são seus principais benefícios para a integração de projetos. 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 1 de 4.
Quanto tempo leva a aula “Introdução ao Git Subtree”?
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 ao Git Subtree
- Adicionando e Mesclando com Subtree
- Comparação entre Subtree e Submódulo
- Enviando alterações de volta à origem com divisão de subtree