BlogArticles/2025-11/不使用Git reset 恢复误删除的 Git Sub...

1.7 KiB
Raw Blame History

title description draft type created_at published_at updated_at category tags tech_stack tech_stack_percent tech_stack_icon_names tech_stack_theme_colors
不使用git reset 恢复误删除的 submodule 这篇文章介绍了两种恢复误删除 Git Submodule 的方法:一种是使用 git reset 的简单方案,另一种是不使用 reset 的详细手动恢复步骤,帮助你在不改变提交历史的情况下修复子模块。 false article 2025-11-16T16:34:11+08:00 2025-11-16T16:48:20+08:00
2025-11-16T16:48:20+08:00
开发
Git
Git
1
mdi:git
#f65f3d

如果你可以使用Git Reset

如果你可以使用 git reset,那么恢复误删除的 Git Submodule 非常简单:

git reset --hard HEAD
git submodule update --init --recursive

如果不能使用Git Reset

如果你不能使用 git reset只想重新checkout误删除的Git Submodule可以使用以下方法 这里我使用example作为submodule名字和路径.gitmodules 配置如下:

[submodule "example"]
    path = example
    url = https://github.com/example/example

使用以下命令:

git ls-tree HEAD example

能得到类似如下结果:

160000 commit c2459db584158403a05821f8a56327b9936cdbe1  example

据此执行以下命令恢复目标submodule

git update-index --add --cacheinfo 160000,c2459db584158403a05821f8a56327b9936cdbe1,example
git submodule sync --recursive
git submodule update --init --recursive example

这种恢复方法相较于重新执行git submodule add命令不会出现新的submodule commit ID。