主干开发与持续集成
探索主干开发这一轻量级替代方案,了解短生命周期分支和功能开关如何为团队实现真正的持续集成。
主干开发与持续集成 是 CoddyKit 上的免费 Git Advanced: Monorepo, Submodules & Workflows 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Git Advanced: Monorepo, Submodules & Workflows 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Git Advanced: Monorepo, Submodules & Workflows 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What Is Trunk-Based Development?
Trunk-based development (TBD) is a workflow where all developers integrate into a single shared branch (the trunk, usually main) very frequently, ideally several times a day.
It minimizes long-lived branches and the painful merges they create.
Why Frequent Integration Wins
The longer a branch lives, the more it diverges from trunk and the harder the eventual merge. TBD flips this: by merging tiny changes constantly, conflicts stay small and integration pain nearly disappears.
- Faster feedback
- Fewer merge conflicts
- Always-releasable trunk
Short-Lived Feature Branches
TBD does not forbid branches; it requires them to be short-lived (hours to a day or two). Create, review, merge, delete.
git switch -c add-rate-limit
# small focused change + tests
git push -u origin add-rate-limit
# open PR, review, merge same dayHiding Unfinished Work with Flags
How do you merge incomplete features into trunk safely? Feature flags. Code ships disabled, then is enabled later via configuration without another deploy.
if (flags.isEnabled('new-checkout')) {
renderNewCheckout();
} else {
renderLegacyCheckout();
}Continuous Integration in Practice
True CI means every push runs the full test suite against trunk. A green pipeline is the gate that keeps trunk releasable at all times.
Keeping Trunk Green
A broken trunk blocks the whole team. Common safeguards:
- Required status checks before merge
- Merge queues that re-test against the latest trunk
- A culture of fixing or reverting fast
Merge Queues
When many PRs land at once, each may pass alone but break together. A merge queue serializes merges and re-runs CI on the combined result before it hits trunk.
Small Batches, Small Risk
Smaller commits are easier to review, test, and revert. If something breaks, reverting one tiny change is trivial compared to untangling a giant feature branch.
git revert <bad-commit-sha>TBD vs. Git Flow
Git Flow uses many long-lived branches (develop, release, hotfix). TBD favors one trunk plus ephemeral branches. TBD pairs naturally with continuous delivery; Git Flow suits scheduled, versioned releases.
When TBD Fits
TBD shines with strong automated testing and continuous deployment. Teams without solid CI may struggle, because the trunk's safety depends entirely on fast, reliable checks.
Retiring Stale Flags
Feature flags accumulate. Once a feature is fully rolled out, remove its flag and the dead branch of code. Stale flags add complexity and quietly become a source of bugs.
Quick Check
Test your understanding of trunk-based development.
Recap
You learned trunk-based development: frequent integration into one trunk, short-lived branches, feature flags to hide unfinished work, strong CI and merge queues to keep trunk green, and small batches for low-risk reverts. TBD enables true continuous integration for fast-moving teams.
常见问题解答
「主干开发与持续集成」课时是免费的吗?
是的 — 「主干开发与持续集成」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 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 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「主干开发与持续集成」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Git Advanced: Monorepo, Submodules & Workflows 课中编写并运行代码吗?
能。每节 Git Advanced: Monorepo, Submodules & Workflows 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。