1

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:

enter image description here

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: enter image description here

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.

NoobCoder
  • 513
  • 3
  • 18
  • You can have more than one *remote*, but you cannot call them all `origin`. :-) (Meanwhile see VonC's answer for the actual question/answer.) – torek Apr 29 '21 at 08:00
  • @torek thanks. I did see VonC's answer but didn't really get it. I'll be glad if you'll write your idea. – NoobCoder Apr 29 '21 at 12:41
  • I don't have any particular suggestions here. I do think that submodules aren't right, but aren't wrong either—the concepts in them are sensible but the way they work with Git is too rigid. There's no obvious way to fix that, unfortunately. – torek Apr 29 '21 at 12:50

1 Answers1

0

I don't think it would be done with Git alone:

I would create a script which would regularly copy those files in separate local Git repositories linked to separate GItHub projects.

That script would then check if the status has changed (meaning if the latest copy has introduced any change in the tracked files), and if so, would add, commit and push.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for your answer. What about the first option, can it be done with Git alone in your opinion? Can I even create 2 different local repositories like you said? Lets say the current one that will stay for company's GitLab and create another one that will be linked to GitHub? and then what, `git push` command will push to which one of them from which one of the repositories? – NoobCoder Apr 29 '21 at 07:31
  • Why would I need to check if the status has changed? Don't you think it'll be enough to create a 'soft link` of those files to the new local repository, and then those files will be changed if the original files will be changed, and then i'll just have to create an `alias` of `git dpush` or whatever that will "double push` this file. `alias git dpush="git add + commit + push origin && git add + commit + push origin2"` something like that? – NoobCoder Apr 29 '21 at 07:35
  • @NoobCoder You can create as many local repo (each one linked to their matching GitHub remote repo) as you want, as long they are in their own separate folder, outside your main project. Hence the answer aout a script populating those separate repositories. – VonC Apr 29 '21 at 07:35
  • @NoobCoder Symlink won't work. Just copy them once every hour. Then you can detect changes: https://stackoverflow.com/a/3899339/6309 – VonC Apr 29 '21 at 07:36
  • What do you think about: https://stackoverflow.com/questions/36554810/how-to-link-folder-from-a-git-repo-to-another-repo Do you think this is the key for my solution? – NoobCoder Apr 29 '21 at 08:05
  • @NoobCoder Submodule is for a full repo (https://stackoverflow.com/a/67295418/6309). Subtree might work, but I don't know enough about it to be sure. – VonC Apr 29 '21 at 08:08
  • The problem with "copy them once every hour" is it can easily fail, works 24/7 and uses resources, and if any changes will be made, what will happen? Will it push to Github? Yes. But without the right commit message. What does it mean "Submodule" is for a full repo"? What is the difference between "a full repo" and my case? – NoobCoder Apr 29 '21 at 12:32
  • @NoobCoder Full repo is, as I mentioned in https://stackoverflow.com/a/67295418/6309, all the files, not a subset. – VonC Apr 29 '21 at 13:02