惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
D
DataBreaches.Net
F
Fortinet All Blogs
阮一峰的网络日志
阮一峰的网络日志
博客园_首页
Apple Machine Learning Research
Apple Machine Learning Research
H
Help Net Security
M
MIT News - Artificial intelligence
美团技术团队
人人都是产品经理
人人都是产品经理
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The Cloudflare Blog
有赞技术团队
有赞技术团队
L
LangChain Blog
博客园 - Franky
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 【当耐特】
S
SegmentFault 最新的问题
V
Visual Studio Blog
Blog — PlanetScale
Blog — PlanetScale
Hugging Face - Blog
Hugging Face - Blog
B
Blog
I
InfoQ

流动

山西之旅 | 流动 博客焕新,记录继续 | 流动 友链 | 流动 博客加速实践 广府古城一日游 我做了一个口播短视频二创工具:VideoRemaker Windows 下 Codex Cli 更新报 Move-Item is denied 错误的解决 Caddy 转发 GOST 报 TLS internal error 问题的排查与解决 对口型视频合成方案对比:Wav2Lip、VideoReTalking 与 MuseTalk 2026 音色克隆方案对比:IndexTTS-2、CosyVoice、GPT-SoVITS、Fish Speech、VoxCPM 部署与实测 AI Agent折腾记(OpenClaw / Hermes Agent) 我的2025年 大连之行 回家收麦 六一儿童节爬长城 Golang database/sql 数据库断线自动重连机制解析 Golang默认Http Client导致的cannot assign requested address错误 清明踏春,爬山看海 购入小牛G400T电动车 北京的三月飞雪 wrenAI本地LLM模型部署 天津一日游 2024年终总结 停止使用staticfile.org服务 使用 ImageMagick 自动添加水印,保护图片版权 如何注册一个.sol域名 奥森公园半日游 昌平42公里骑行绿道打卡 十月一日爬慕田峪长城 当Hugo遇上AVIF,优化图片加载
一个git submodule update引发的问题
Liudon · 2019-09-06 · via 流动

背景

1月份的时候,用hugo搭了这套博客系统。

本机写md文件,更新到github,然后通过travis-ci自动发布。

jane主题是通过git submodule引入的,.gitmodules文件内容。

[submodule "themes/jane"]
	path = themes/jane
	url = https://github.com/xianmin/hugo-theme-jane.git

问题

最近几天更新完文章后,发现首页显示出了问题。

一开始以为是主题有问题,具体描述见首页文章不显示了

issue里: shaform提到使用的并不是最新的版本。 RocFang提到是git submodule使用的问题。

但是travis-ci每次都是通过git submodule update --init --recursive更新子仓库代码的,为什么会不是最新的代码呢。

问题重现

接下来,我们用一个新的仓库,来模拟重现一下。

  1. 克隆仓库。

    [root@VM_81_18_centos xx]# git clone git@github.com:Liudon/test.git
    Cloning into 'test'...
    [root@VM_81_18_centos test]# 
    
  2. 添加文件。

     [root@VM_81_18_centos xx]# cd test/
     [root@VM_81_18_centos test]# echo "# test" >> README.md
     [root@VM_81_18_centos test]# git add README.md
     [root@VM_81_18_centos test]# 
    
  3. 引用子仓库。

     [root@VM_81_18_centos test]# git submodule add git@github.com:xianmin/hugo-theme-jane.git theme/jane
     Cloning into 'theme/jane'...
     remote: Enumerating objects: 216, done.
     remote: Counting objects: 100% (216/216), done.
     remote: Compressing objects: 100% (128/128), done.
     remote: Total 6165 (delta 102), reused 159 (delta 65), pack-reused 5949
     Receiving objects: 100% (6165/6165), 3.05 MiB | 1.70 MiB/s, done.
     Resolving deltas: 100% (3443/3443), done.
    
  4. 查看文件列表。

     [root@VM_81_18_centos test]# ll
     total 8
     -rw-r--r-- 1 root root    5 Sep  6 16:05 README.md
     drwxr-xr-x 7 root root 4096 Sep  6 16:08 typecho
     [root@VM_81_18_centos test]# 
    
  5. 查看状态。

    [root@VM_81_18_centos test]# git status
    # On branch master
    #
    # Initial commit
    #
    # Changes to be committed:
    #   (use "git rm --cached <file>..." to unstage)
    #
    #	new file:   .gitmodules
    #	new file:   README.md
    #	new file:   typecho
    #
    [root@VM_81_18_centos test]# 
    
  6. 查看修改。

    [root@VM_81_18_centos test]# git diff --cached
    diff --git a/.gitmodules b/.gitmodules
    new file mode 100644
    index 0000000..b1ddf70
    --- /dev/null
    +++ b/.gitmodules
    @@ -0,0 +1,3 @@
    +[submodule "typecho"]
    +       path = typecho
    +       url = https://github.com/Liudon/typecho
    diff --git a/README.md b/README.md
    new file mode 100644
    index 0000000..9daeafb
    --- /dev/null
    +++ b/README.md
    @@ -0,0 +1 @@
    +test
    diff --git a/typecho b/typecho
    new file mode 160000
    index 0000000..b0c4cc7
    --- /dev/null
    +++ b/typecho
    @@ -0,0 +1 @@
    +Subproject commit b0c4cc77a7f8f04661fb9f75d4ba6d4d7915b0f1
    [root@VM_81_18_centos test]# 
    

    注意最后一行Subproject commit b0c4cc77a7f8f04661fb9f75d4ba6d4d7915b0f1

    这个commitId是子仓库最新提交的记录id,对应的修改记录

  7. 提交修改。

    [root@VM_81_18_centos test]# git push -u origin master
    Counting objects: 4, done.
    Compressing objects: 100% (3/3), done.
    Writing objects: 100% (4/4), 362 bytes | 0 bytes/s, done.
    Total 4 (delta 0), reused 0 (delta 0)
    To git@github.com:Liudon/test.git
    * [new branch]      master -> master
    Branch master set up to track remote branch master from origin.
    [root@VM_81_18_centos test]# 
    

    提交后,在github上子仓库后面会多显示一个@xxxxx,这里就是引用的commitId,对应到前面git diff最后一行。

    点击查看提交记录

    本次提交的commitId5b11d515db8ad8d299ef1691f115590e0015c3b7,子仓库typecho单独记录了引入时的commitId,为b0c4cc77a7f8f04661fb9f75d4ba6d4d7915b0f1,对应的提交记录

  8. 接下来克隆子仓库,进行更新提交。

    [root@VM_81_18_centos xx]# git clone git@github.com:Liudon/typecho.git
    Cloning into 'typecho'...
    remote: Enumerating objects: 1, done.
    remote: Counting objects: 100% (1/1), done.
    remote: Total 7179 (delta 0), reused 0 (delta 0), pack-reused 7178
    Receiving objects: 100% (7179/7179), 7.26 MiB | 2.02 MiB/s, done.
    Resolving deltas: 100% (4844/4844), done.
    [root@VM_81_18_centos xx]# 
    [root@VM_81_18_centos xx]# cd typecho/
    [root@VM_81_18_centos typecho]# git log -n 1
    commit b0c4cc77a7f8f04661fb9f75d4ba6d4d7915b0f1
    Merge: c904005 8fd7492
    Author: 祁宁 <magike.net@gmail.com>
    Date:   Tue Nov 18 13:59:52 2014 +0800
    
        Merge branch 'master' of https://github.com/typecho/typecho
    [root@VM_81_18_centos typecho]#
    

    通过git log,确认最新的提交commitId为b0c4cc77a7f8f04661fb9f75d4ba6d4d7915b0f1,与前面的引入的一致。

    [root@VM_81_18_centos typecho]# echo "xxx" > test
    [root@VM_81_18_centos typecho]# 
    [root@VM_81_18_centos typecho]# git add test
    [root@VM_81_18_centos typecho]# git commit -m 'test'
    [master 5dcc8f4] test
    1 file changed, 1 insertion(+)
    create mode 100644 test
    [root@VM_81_18_centos typecho]# git push
    Counting objects: 4, done.
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (3/3), 252 bytes | 0 bytes/s, done.
    Total 3 (delta 1), reused 0 (delta 0)
    remote: Resolving deltas: 100% (1/1), completed with 1 local object.
    To git@github.com:Liudon/typecho.git
    b0c4cc7..5dcc8f4  master -> master
    [root@VM_81_18_centos typecho]# 
    

    修改文件提交。

    [root@VM_81_18_centos typecho]# git log -n 1
    commit 5dcc8f4e91cc724ba82aba5b9e7955727b58c5c2
    Author: liudon <i.mu@qq.com>
    Date:   Fri Sep 6 16:26:47 2019 +0800
    
        test
    [root@VM_81_18_centos typecho]#
    

    最新提交的commitId5dcc8f4e91cc724ba82aba5b9e7955727b58c5c2

  9. 重新克隆test库。

    [root@VM_81_18_centos yy]# git clone git@github.com:Liudon/test.git
    Cloning into 'test'...
    remote: Enumerating objects: 4, done.
    remote: Counting objects: 100% (4/4), done.
    remote: Compressing objects: 100% (3/3), done.
    remote: Total 4 (delta 0), reused 4 (delta 0), pack-reused 0
    Receiving objects: 100% (4/4), done.
    [root@VM_81_18_centos yy]# cd test/
    [root@VM_81_18_centos test]# ll
    total 8
    -rw-r--r-- 1 root root    5 Sep  6 16:31 README.md
    drwxr-xr-x 2 root root 4096 Sep  6 16:31 typecho
    [root@VM_81_18_centos test]# ll typecho/
    total 0
    [root@VM_81_18_centos test]# 
    

    这里可以看到typecho目录下是没有文件的。

    [root@VM_81_18_centos test]# git submodule update --init --recursive
    Submodule 'typecho' (https://github.com/Liudon/typecho) registered for path 'typecho'
    Cloning into 'typecho'...
    remote: Enumerating objects: 4, done.
    remote: Counting objects: 100% (4/4), done.
    remote: Compressing objects: 100% (3/3), done.
    remote: Total 7182 (delta 0), reused 2 (delta 0), pack-reused 7178
    Receiving objects: 100% (7182/7182), 7.26 MiB | 1.26 MiB/s, done.
    Resolving deltas: 100% (4844/4844), done.
    Submodule path 'typecho': checked out 'b0c4cc77a7f8f04661fb9f75d4ba6d4d7915b0f1'
    [root@VM_81_18_centos test]#
    

    更新子仓库代码,这里可以看到最终checkout的版本为b0c4cc77a7f8f04661fb9f75d4ba6d4d7915b0f1,与前面提交时的版本一致。

问题分析

git submodule add的时候,会记录当时引入时子仓库的版本id。

git submodule update --init --recursive,会检出引入时的仓库版本,这就是为啥代码没有更新了。

问题解决

[root@VM_81_18_centos yy]# git clone git@github.com:Liudon/test.git
Cloning into 'test'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 4 (delta 0), pack-reused 0
Receiving objects: 100% (4/4), done.
[root@VM_81_18_centos yy]# 
[root@VM_81_18_centos yy]# 
[root@VM_81_18_centos yy]# cd test/
[root@VM_81_18_centos test]# ll
total 8
-rw-r--r-- 1 root root    5 Sep  6 16:37 README.md
drwxr-xr-x 2 root root 4096 Sep  6 16:37 typecho
[root@VM_81_18_centos test]# ll typecho/
total 0
[root@VM_81_18_centos test]# 
[root@VM_81_18_centos test]# git submodule update --init --remote --recursive
Submodule 'typecho' (https://github.com/Liudon/typecho) registered for path 'typecho'
Cloning into 'typecho'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 7182 (delta 0), reused 2 (delta 0), pack-reused 7178
Receiving objects: 100% (7182/7182), 7.26 MiB | 1.24 MiB/s, done.
Resolving deltas: 100% (4844/4844), done.
Submodule path 'typecho': checked out '5dcc8f4e91cc724ba82aba5b9e7955727b58c5c2'
[root@VM_81_18_centos test]#

使用git submodule update --init --remote --recursive命令。