gitnestedgit-submodules

Force git to treat a nested repository as a regular folder instead of submodule but still keep it as a git repository


This is an example directory structure

ParentRepo/
    .git/
    parent_file
    ChildRepo/
        .git/
        child_file
        child_untracked

I want ParentRepo to track changes of all files under itself (track parent_file, child_file, child_untracked, but don't track ChildRepo/.git) as if ChildRepo was not a git repository, but I need to keep ChildRepo as an git repository

There are three hard requirements

Currently, git treats any directory with a .git/ subdirectory as a git repo and I need to bypass this behavior

These are the reasons for the weird conflicting requirements


Solution

  • Untrack it as a submodule with git rm --cached, then re-add it as a subdirectory (or rather, add some individual files) using low-level tools:

    git update-index --add ChildRepo/child_file
    

    It used to be possible to simply do:

    git add ChildRepo/
    

    The trailing / is important, causing the contents rather than the repository to be added. Unfortunately, in current Git versions this seems to no longer work.

    Note that you won't be able to add a .git subdirectory even if you tried – the exclusion is hardcoded. (But you could git fetch its branches into the parent to keep a local copy.)