最近在弄hugo搭博客,涉及到了git的submodule操作,在这里做一个记录,如何正确的操作git的submodule

移除子模块

To remove a submodule you need to:

  1. Delete the relevant section from the .gitmodules file.
  2. Stage the .gitmodules changes git add .gitmodules
  3. Delete the relevant section from .git/config.
  4. Run git rm –cached path_to_submodule (no trailing slash).
  5. Run rm -rf .git/modules/path_to_submodule (no trailing slash).
  6. Commit git commit -m “Removed submodule "
  7. Delete the now untracked submodule files rm -rf path_to_submodule

I think above can be simplified using following commands:

git submodule deinit <path_to_submodule>
git rm <path_to_submodule>
git commit-m "Removed submodule "
rm -rf .git/modules/<path_to_submodule>

删除完后,就可以重新添加submodule了

克隆项目

  1. 克隆包含子模块代码: git clone --recursive [URL to Git repo]
  2. 如果已经拉取了代码
    git submodule update --init
    # if there are nested submodules:
    git submodule update --init --recursive
    

更新

git submodule update --remote --merge

资料

  1. 官网
  2. gist
  3. Using submodules in Git - Tutorial