Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 209
HostileFork says dont trust SE Avatar asked Jan 12 '14 16:01

HostileFork says dont trust SE


People also ask

How do I clone a project with submodules?

Cloning a Project with SubmodulesIf you pass --recurse-submodules to the git clone command, it will automatically initialize and update each submodule in the repository, including nested submodules if any of the submodules in the repository have submodules themselves.

Can Symlinks be pushed to git?

Git can track symlinks as well as any other text files. After all, as the documentation says, a symbolic link is nothing but a file with special mode containing the path to the referenced file.

How do I combine submodules?

In order to update an existing Git submodule, you need to execute the “git submodule update” with the “–remote” and the “–merge” option. Using the “–remote” command, you will be able to update your existing Git submodules without having to run “git pull” commands in each submodule of your project.

Can a submodule point to a branch?

You can set the submodule to track a particular branch (requires git 1.8. 2+), which is what we are doing with Komodo, or you can reference a particular repository commit (the later requires updating the main repository whenever you want to pull in new changes from the module – i.e. updating the commit hash reference).


2 Answers

You can bind mount the submodule's other directory

sudo mount --bind /path/to/main/repo relative/path/to/submodule 

I can't find much info about this method, but saw it listed in this, similar question.

like image 88
Gilly Avatar answered Sep 28 '22 10:09

Gilly


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 more 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.
like image 40
VonC Avatar answered Sep 28 '22 10:09

VonC