Search code examples
gitsymlinkgit-submodules

How to use submodules publicly, but symlinks to a single clone locally?


I'm having my first Git Submodule experience.

I have some projects that depend on the same subproject. I keep these projects in sync, so I'm using the "submodule branch" feature (e.g. git submodule add -b master [URL]).

While I'd like the public GitHub repositories to convey the submodule relationship, in my own workflow I'd really just like to have one clone of the shared codebase on my disk. I thought I could just set up the submodules, and then do a switcheroo with a symbolic link. But when I do, I get this:

On branch master
Changes not staged for commit:
 (use "git add <file>..." to update what will be committed)
 (use "git checkout -- <file>..." to discard changes in working directory)

    typechange: draem

So Git apparently sees the fact that it's a symbolic link, instead of following through to the directory.

Is there any workflow where I can appear to be working with submodules, but really only have one clone on my local filesystem?


Solution

  • So Git apparently sees the fact that it's a symbolic link, instead of following through to the directory.

    Yes, Git would see such a change, because that submodule is declared in the parent repo as a special entry in the index.

    Making a symlink would replace that special entry by a file of another type.

    What you could do is try playing with GIT_WORK_TREE (as in "Including submodules in git checkout to GIT_WORK_TREE in hook").

    But a simpler solution would be to:

    • keep your submodule right where they are.
    • add another clone of that submodule repo where you want it (/path/to/sub).
    • detect any changes from the original submodule folder with a git --work-tree=/path/to/sub status from within your duplicated submodule folder in your parent repos.

    It will detect changes between:

    • the submodule repository HEAD (the original submodule folder, where you execute the git --work-tree=/path/to/sub status command)
    • the working tree you are designated (--work-tree=/path/to/sub)

    You can:

    • work from your submodule cloned repository outside your main parent repository (Ie.: where you want)
    • detect changes done in that outside folder right from your submodule folder of your main parent repository (the one which references all your submodules)