0Pricing
Git Advanced: Monorepo, Submodules & Workflows · 课时

使用子模块分支

学习如何管理和切换子模块中的分支,并将更改推送回子模块的源仓库

使用子模块分支 是 CoddyKit 上的免费 Git Advanced: Monorepo, Submodules & Workflows 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Git Advanced: Monorepo, Submodules & Workflows 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Git Advanced: Monorepo, Submodules & Workflows 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Branching in Submodules

Git submodules usually point to a specific commit. But what if you need to actively develop inside a submodule?

  • This allows independent feature development.
  • You can contribute changes back to the submodule's own project.
  • It's key for complex projects needing tight integration.

Submodules: Detached HEAD

By default, when you clone a project with submodules, each submodule is checked out to a specific commit. This is called a "detached HEAD" state.

  • It means you're not currently on any named branch.
  • You can view files, but new commits won't automatically belong to a branch.
  • It provides a stable way for the parent repo to reference specific submodule versions.

Navigating into a Submodule

To work with a submodule's branches, you first need to navigate into its directory. Think of it as entering a completely separate Git repository.

Use the cd command to change directories:

# Assume 'my-submodule' is a submodule
cd my-submodule
git status

Checking Submodule Status

Once inside the submodule's directory, you can use regular Git commands to see its status, branches, and current HEAD. This helps you understand its current state.

# Inside the submodule directory
git status
git branch
git log --oneline -1

Switching Submodule Branches

To develop within a submodule, you need to switch from a detached HEAD to a specific branch. Use git checkout just like in any other Git repository.

# Inside the submodule directory
git checkout main
# Or to create and switch to a new branch
git checkout -b new-feature

Committing in a Submodule

Now that you're on a branch, you can make changes, add them, and commit them. These commits are specific to the submodule's repository, not the parent repository.

# Inside the submodule directory
echo "New content" > newfile.txt
git add newfile.txt
git commit -m "Added newfile in submodule"

Pushing Submodule Changes

After committing changes within the submodule, you need to push them to the submodule's own remote repository. This makes your changes available to others.

# Inside the submodule directory
git push origin main
# Or to push a new branch
git push origin new-feature

Updating the Parent Repository

After pushing changes in the submodule, the parent repository still points to the old commit. You need to tell the parent repo to track the new commit.

  • Navigate back to the parent repository.
  • Stage and commit the submodule's change.
# Back in the parent repository
cd .. # Go up one level
git status
git add my-submodule
git commit -m "Updated my-submodule to latest commit"
git push origin main

Submodule Branching Tips

Working with submodule branches requires care and coordination:

  • Communicate: Inform your team if you're actively developing on a submodule branch.
  • Keep parent updated: Always commit the submodule reference change in the parent repo after pushing submodule changes.
  • Avoid direct pushes: Don't push to a submodule's main branch from the parent repo unless absolutely necessary; work inside the submodule itself.

Submodule Branch Quiz

Test your knowledge on managing submodule branches!

Recap: Submodule Branches

In this lesson, we explored how to actively develop within a Git submodule:

  • Navigating into the submodule's directory.
  • Switching from a detached HEAD to a specific branch.
  • Making, committing, and pushing changes in the submodule.
  • Updating the parent repository's reference to the new submodule commit.

This workflow enables independent development and contribution to submodule projects, making your overall project more flexible.

常见问题解答

「使用子模块分支」课时是免费的吗?

是的 — 「使用子模块分支」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 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 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「使用子模块分支」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Git Advanced: Monorepo, Submodules & Workflows 课中编写并运行代码吗?

能。每节 Git Advanced: Monorepo, Submodules & Workflows 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用子模块分支
  2. 嵌套子模块与复杂配置
  3. 常见子模块问题与修复
  4. 将子模块固定到特定标签与提交
← 返回 Git Advanced: Monorepo, Submodules & Workflows