118

I want to create a public repo to put some sample files from my main repo (private). Is there any way to soft link few folders from a git repo to another git repo?

Manoj
  • 1,833
  • 3
  • 14
  • 11

2 Answers2

197

Then you should use submodules for this task.

Submodule are different git repositories under the same root.
This way you can manage 2 different project at folder level inside the root repository

Submodules allow foreign repositories to be embedded within a dedicated subdirectory of the source tree, always pointed at a particular commit.


git submodule

Break your big project to sub projects as you did so far.
Now add each sub project to you main project using :

git submodule add <url>

Once the project is added to your repo, you have to init and update it.

git submodule init
git submodule update

As of Git 1.8.2 new option --remote was added

git submodule update --remote --merge

will fetch the latest changes from upstream in each submodule, merge them in, and check out the latest revision of the submodule.

As the docs describe it:

--remote

This option is only valid for the update command. Instead of using the superproject’s recorded SHA-1 to update the submodule, use the status of the submodule’s remote-tracking branch.

This is equivalent to running git pull in each submodule.


However, how would I push a commit in the scenario of bug fix in C which affects the code shared with the parent layers?

Again: using submodule will place your code inside your main project as part of its content. The difference between having it locally inside the folder or having it as part of a submodule is that in submodule the content is managed (commited) to a different standalone repository.


This is an illustration of submodule - project inside another project in which each project is a standalone project.

enter image description here


git subtree

Git subtree allows you to insert any repository as a sub-directory of another one

Very similar to submodule but the main difference is where your code is managed. In submodules the content is placed inside a separate repo and is managed there which allow you to clone it to many other repos as well.

subtree is managing the content as part of the root project and not in a separate project.

Instead of writing down how to set it up and to understand how to use it you can simply read this excellent post which will explain it all.

https://developer.atlassian.com/blog/2015/05/the-power-of-git-subtree/

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • 10
    Great reference to SubModules and SubTrees. I don't see that you explicitly addressed referencing a subdirectory of the source repository in the destination repository. – skitheo May 16 '17 at 00:14
  • It will be great if you can add a code sample. – mdabdullah May 06 '21 at 12:18
  • Great answer! I have one corollary question to this thread :) Instead of importing repository A under repository B, is it possible to only import a part of repository A? for example, B should contain a specific folder from repository A, instead of the whole A. – gab Aug 30 '22 at 15:39
  • yet, you can also do partial import, find my email in my info and ill help u – CodeWizard Aug 31 '22 at 05:57
  • A disadvantage of submodules is that if I change the content of the submodule, I need to commit twice, the submodule and the parent project. Also see related question https://stackoverflow.com/questions/76454282/how-to-sync-a-public-subfolder-of-one-repository-with-another-repository/76462303#76462303 – Stefan Jun 13 '23 at 12:55
-3

I am answering your X problem not your Y problem (xyproblem.info); you should not use submodules for this task. You should create a .gitignore to exclude the secrets from VCS. Alternatively, you could make the code read the config files from outside the VCS directory, so you can keep them in ~/.config. Storing config files in a private repo is almost never the right way.

The answer by @CodeWizard answers the Y problem perfectly.

Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
Hack5
  • 3,244
  • 17
  • 37
  • 8
    The OP didn't say that he wanted to store secrets in another repo (instead, he has a private, i.e. proprietary, repo and he wants to expose a subset of this repo, containing some sample code). But you make a good general point. – Jonathan Fischer Nov 19 '19 at 20:12