使用子树添加与合并
学习将外部仓库添加为子树的命令,以及如何从其上游仓库拉取更新
使用子树添加与合并 是 CoddyKit 上的免费 Git Advanced: Monorepo, Submodules & Workflows 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Git Advanced: Monorepo, Submodules & Workflows 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Git Advanced: Monorepo, Submodules & Workflows 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Subtree: Integrating External Code
Welcome! In this lesson, we'll dive into Git Subtree, a powerful way to integrate another Git repository directly into your project.
Unlike submodules, subtree merges the external repository's history into your own, making it feel like a native part of your project.
Why Use `git subtree add`?
Subtree offers a simpler workflow than submodules for many cases. Here are a few benefits:
- No special commands: Once added, you work with subtree content like regular files.
- Single repository: No separate
.gitdirectories or complex submodule initializations. - Easier for consumers: Others cloning your project don't need to learn special submodule commands.
Preparing Your Repository
Before adding a subtree, ensure your main repository is in a clean state (no uncommitted changes). You'll need:
- The URL of the external repository.
- The branch or commit you want to integrate (e.g.,
mainormaster). - A chosen path within your main repository for the subtree.
The `git subtree add` Command
To add an external repository as a subtree, you use the git subtree add command. It looks like this:
git subtree add --prefix=<path> <repository> <ref>
--prefix=<path>: The directory in your project where the subtree will reside.<repository>: The URL of the external Git repository.<ref>: The branch (e.g.,main) or commit hash from the external repo.
Adding an Example Subtree
Let's say you want to add a shared utility-library from a separate Git repository into a src/shared folder in your current project. You would use:
git subtree add --prefix=src/shared/utility-library https://github.com/myorg/utility-library.git main
This command fetches the utility-library's main branch and integrates its history into your src/shared/utility-library directory.
Using the `--squash` Option
When adding a subtree, you'll often see the --squash option:
git subtree add --prefix=path repo_url main --squash
The --squash flag combines all commits from the external repository's history into a single commit in your main project. This keeps your main project's history cleaner, but you lose the individual commit details from the subtree's original history.
Why Update a Subtree?
After you've added a subtree, the original external repository might receive new updates. To keep your integrated code current with the upstream source, you'll need to pull those changes.
This ensures your project always has the latest versions of the shared components or libraries you've included as a subtree.
The `git subtree pull` Command
To pull updates from an upstream subtree, you use a similar command structure:
git subtree pull --prefix=<path> <repository> <ref>
Notice it uses the same --prefix, <repository>, and <ref> as when you added it. Git uses these to identify the correct upstream source for your subtree.
Updating the Example Subtree
Let's continue with our utility-library example. To pull the latest changes from its main branch into your project, you'd run:
git subtree pull --prefix=src/shared/utility-library https://github.com/myorg/utility-library.git main
This command fetches and merges any new commits from the utility-library's main branch into your local src/shared/utility-library directory.
Subtree Command Check
Which command is used to initially incorporate an external repository as a subtree into your project, creating a new directory for it?
Recap: Adding & Merging Subtrees
You've learned how to integrate external repositories using Git Subtree!
- Use
git subtree addwith--prefix, repository URL, and branch to bring external code into your project. - The
--squashoption helps keep your main project's history clean. - Use
git subtree pullwith the same parameters to fetch and merge upstream updates into your subtree.
Subtree provides a powerful, integrated alternative to submodules for managing shared code!
常见问题解答
「使用子树添加与合并」课时是免费的吗?
是的 — 「使用子树添加与合并」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Git Advanced: Monorepo, Submodules & Workflows 课程的其余内容,请升级到 CoddyKit PRO。 Git Advanced: Monorepo, Submodules & Workflows 课程共包含 4 节课。
「使用子树添加与合并」这节课中我会学到什么?
学习将外部仓库添加为子树的命令,以及如何从其上游仓库拉取更新 你通过在浏览器中直接运行的动手代码来练习 Git Advanced: Monorepo, Submodules & Workflows,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Git Advanced: Monorepo, Submodules & Workflows 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Git Advanced: Monorepo, Submodules & Workflows 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「使用子树添加与合并」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Git Advanced: Monorepo, Submodules & Workflows 课中编写并运行代码吗?
能。每节 Git Advanced: Monorepo, Submodules & Workflows 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Git 子树入门
- 使用子树添加与合并
- 子树与子模块对比
- 使用子树拆分将变更推送回上游