Multiple origins and access different local directories

I know a project can have two or more remote origins in Git.

I got a GitLab account from the company I work for, and I want to set up a github that will receive the projects that I work on.

The problem is that I don’t want to allow GitHub to copy ALL the work’s repository, I want it to sync with only one folder inside it.

Let me draw it to be clear:

(sorry I can add only one embedded media):

The orange is the current situation.
The green is what I wish for.

I want the GitHub repository to include ONLY one folder from the local repository, so when i’ll update any files in that folder, both repositories, GitHub and GitLab will be updated. But the GitHub repo won’t even know or gain access to the other folders inside of ~/work.

Is it possible?

Also, can it be even more seperated?
For example, let me draw it again:

I mean I wish I could “link” files by their name and just create a new project every time in Github, so whenever i’ll update files: stack.c, stack.h the project Stack in GitHub will be updated, but also company’s GitLab at ~work/projects/c/stack.c and ~work/projects/h/stack.h

Can it be done?
One of those options?

Thanks.

:wave: Welcome!

I’m no git guru so I’m happy to stand corrected but I think the short answer is no, not in the way you describe and the longer answer is maybe!

In general you can’t separate parts of commits, so if you changed both your projects folder and other things, then you can’t push part of the commit to one place while pushing the entire commit elsewhere.

I suspect the cleanest and most secure way to accomplish this separation is using submodules. In this case, you’ll break “Projects” out of the main repository altogether and make it its own repository. You are now free to push that anywhere you want, to one or many remotes, whatever.

I found some instructions here that seem pretty complete - just remember to always work on a copy of your repository when trying destructive actions.

When you have Projects as a separate repository, you’ll be able to use it in another repository using submodules.

1 Like