0

Let's say I have a repository test:

test.git

src/
    hello/
        awesome.c
    nice/
        cool.c
        fun.c
.gitignore
README.md

I would like to add src/hello/ to another repository (exactly like a submodule but not the main path (/src/hello/ instead of /)

test2.git

ok/
hello/ (submodule - links to test/src/hello)
    awesome.c
.gitmodules
.gitignore

Something like: (test2 git shell)

$ git submodule add https://github.com/username/test/src/hello

lordcommander
  • 320
  • 3
  • 7
  • Sounds like you may want to [sparse checkout your submodule](https://stackoverflow.com/questions/6238590/set-git-submodule-to-shallow-clone-sparse-checkout) – Cory Kramer May 11 '21 at 19:12
  • Git does not support this. You can, however, add the entire repository as a submodule and use some sort of checkout script and/or symbolic link trickery (the symlinks have to be in the superproject unless you have direct control of the submodule). – torek May 12 '21 at 01:07

1 Answers1

0

To add a specific directory from a submodule repository, you can follow these steps:

Navigate to the root directory of your main repository, test2.git, in the command line or terminal.

Initialize the repository as a submodule by running the following command:

git submodule init

Open the .gitmodules file in your favorite text editor.

Add the following lines to the .gitmodules file:

[submodule "hello"]
    path = hello
    url = https://github.com/username/test/src/hello

Here, "hello" is the path where you want to place the submodule within test2.git.

Save and close the .gitmodules file.

Run the following command to clone the submodule repository and link it to the desired path within test2.git:

git submodule update --remote

This command clones the submodule repository defined in .gitmodules and places it at the specified path.

Commit the changes to your main repository:

git add .
git commit -m "Added submodule from test repository"

Now, you have added the specific directory src/hello/ from the test repository as a submodule within the hello path in the test2.git repository.