How to keep folder structure while share common code across projects in git?

I have the following directory structure that I'd like to keep it while share some of the directories across different projects. I'd like to achieve something like this:

root
  |_dir1  (git repo1)
  |_dir2  (git repo1)
  |_dir3  (git repo1)
  |_project<n> (git repo2 contains project specific code in different branches, e.g. project1's code in branch1 and project2's code in branch2, etc.)

dir1...3 are common code shared across different projects and I'd like to keep the folder structure unchanged.

I'm thinking about using git submodules but not sure how to do it. Is it possible to put dir1...3 in git repo1, as a submodule, then all project codes in project folder in git repo2, which is the main git repo that hosts git repo1 as the submodule?


Is it possible to put dir1...3 in git repo1, as a submodule, then all project codes in project folder in git repo2, which is the main git repo that hosts git repo1 as the submodule?

That's totally feasible. But the .git of the master project have to be in the root directory (it's up to you to say if it's good for you or not), since submodules have to be located inside the repo's path (which is defined by the location of the .git folder).

From root :

git init
git submodule add <remote_url> dir1
git submodule add <remote_url> dir2
git submodule add <remote_url> dir3
mkdir project<n>
git submodule update --init --recursive
git commit -m "Added the submodule to the project."