Monday, May 11, 2020

Add an existing sub-directory as a git submodule

Use case:

- there is a project directory with an initialized git repo inside
- there is a sub-directory with another initialized git repo inside

Goal:

add the subdirectory as a git submodule to the root project repo

Steps:

  1. Add the submodule's url to .gitmodules and to .git/config with
    git submodule add --name <submodule_name> <url> <path_to_subdir>
  2. Move the submodule's $GIT_DIR directory (.git in regular repositories) to .git/modules/<path> with
    git submodule absorbgitdirs <path_to_subdir>