git 子模块与 submodule 命令详解

背景:学习 Hugo + GitHub Pages 搭建博客,在「安装 PaperMod 主题」这一步遇到了 git submodule 命令。 ⚠️ 注意:文中的 PaperMod 主题仓库地址务必以官方为准,使用前请先核对最新地址: 官方仓库:https://github.com/adityatelange/hugo-PaperMod SSH 地址:git@github.com:adityatelange/hugo-PaperMod.git 曾误写过 adnanhob/PaperMod(该仓库不存在),已更正。仓库地址可能随时间迁移,动手前建议到 GitHub 搜索 hugo-PaperMod 确认。 一、命令逐词拆解 git submodule add --depth=1 git@github.com:adityatelange/hugo-PaperMod.git themes/PaperMod 片段 含义 git submodule add git 的核心命令:添加一个子模块 --depth=1 只拉取最近 1 次提交(浅克隆) git@github.com:adityatelange/hugo-PaperMod.git 主题的远程仓库地址(SSH 形式) themes/PaperMod 主题下载到本地的位置 二、核心概念:什么是「子模块」(submodule) 要解决的场景冲突 你的博客站点本身是个 git 仓库(my-blog),而 PaperMod 主题也是一个独立的 git 仓库。现在你想把主题代码放进博客的 themes/ 目录里。 如果直接 git clone 主题进去,就会形成「仓库套仓库」的混乱局面——博客的 git 会看到 themes/PaperMod 里还有个 .git,管理起来一团糟。 子模块的机制 子模块就是 git 专门用来解决「仓库里套仓库」的机制。做法很聪明: ...

September 20, 2026