Search code examples
gitshellgit-submodules

Add existing git repo as submodule


I am reorganizing my dotfiles and have the following directory structure:

dotfiles
├── .vim
│   ├── colors/
│   ├── ftplugin/
│   ├── pack/
│   │   └── my_plugins/
│   │       ├── opt/
│   │       │   ├── nerdtree/
│   │       │   │   ├── .git
│   │       │   └── vimwiki/
│   │       │       ├── .git
│   │       └── start/
│   │           ├── fugitive/
│   │           │   ├── .git
│   │           ├── syntastic/
│   │           │   ├── .git
│   │           ├── vimade/
│   │           │   ├── .git
│   │           └── vim-css-color/
│   │               ├── .git
│   ├── README.md
│   └── vimrc

There are six directories which are github repos and that have already been cloned to their directories.

I want to now make the entire dotfiles directory version controlled. How do I add the six .../.git repos as submodules to the top level repo, without having to re-clone the packages.

Note that here, a similar question was asked, but the instructions given are unclear. What I need is the step-by-step solution to add the entire .vim directory to the top-level repo.


Solution

  • Here's a step by step process :

    # dotfiles directory is current directory
    git init 
    
    # Add already cloned repos
    git submodule add <github-repo1-link> <path-to-already-cloned-repo1>
    git submodule add <github-repo2-link> <path-to-already-cloned-repo2>
    .
    .
    .
    
    # Add other files for staging
    git add .
    
    # Commit the changes
    git commit
    

    As mentioned in git docs for submodules here, the add command of submodule takes path as an optional argument.

    git submodule add <repository> [<path>]
    

    The optional argument <path> is the relative location for the cloned submodule to exist in the superproject. If <path> is not given, the canonical part of the source repository is used ("repo" for "/path/to/repo.git" and "foo" for "host.xz:foo/.git"). If <path> exists and is already a valid Git repository, then it is staged for commit without cloning. The <path> is also used as the submodule’s logical name in its configuration entries unless --name is used to specify a logical name.