0

I have a private git repository in which I have my own project and some 3rd party projects. This is its rough structure:

Root
|- My Project
|  |-- myproject
|      |-- __init__.py
|      |-- __main__.py
|  |-- tests
|  |-- setup.py
|- Project1
|- Project2

However, if I want to push this to a public repository, I cannot include the 3rd party projects. So I have to push my project in a different state to the public repository.

I've thought of a few solutions so far but they each have their drawbacks:

  1. Using a public branch, to which I only add my own code files. I then push the public branch to the public repo and the master branch to my private repo. However, this solution results in the public repository containing a single folder, rather than looking like a regular Python project (see for example numpy's git repository). I would like a solution that has My Project as the root directory for the public repo.

  2. Checking out the public branch, then moving My Project to the root and deleting everything else. With this approach, I'm not sure how merging development from the master branch would work though.

  3. Using a git subtree. However, this seems to have the same problems as using a separate branch.

  4. Using an actual subrepository by running git init inside MyProject. This solves the problem of the repo root, but it makes the public repository the default one when the MyProject directory is the CWD. So git (and any version control modules in the development tools I use that rely on git, such as VSCode's) will commit and push to the public repository instead of the private one. I want most of the development to be pushed to the private repo with only the occasional (deliberate) push to the public repo.

  5. Using a submodule. However, the way I understand it, submodules are intended for use when you want to independently develop a part of your repository, which is kind of the opposite of what I'm trying to achieve (I want to develop my repository as a whole and only occasionally push a part of it to the public repo). Additionally, the submodule would require specific handling on subsequent cloning of the private repo. Last, I've read that submodules are rarely a good idea (see for instance the first sentence of https://www.atlassian.com/git/tutorials/git-subtree).

Is there a solution that will let me connect my local repository to 2 remote ones (private and public), such that:

  • The public repo will contain only my own code from My Project;
  • The public repo will be rooted at My Project;
  • Any development I do will by default be added, committed and pushed to the private repository (for instance, if using the exact commands git add -A; git commit -m "msg"; git push);
  • Cloning the private repo with git clone path-to-remote-private-repo will clone the entire project (including the content of My Project);
  • I'll be able to seamlessly merge changes to the code in My Project to the public distro;
  • (optionally) I'll be able to, on a file-by-file basis, add certain files from Project1 and Project2 as extra files to the public distro. I can probably do this easily using symlinks but maybe there's a better way.
Mate de Vita
  • 1,102
  • 12
  • 32
  • As for the public vs private repository issue; just make two remotes, (e.g. one called private, one public) and push to the one you want to push to. – TamaMcGlinn Apr 02 '20 at 14:59
  • 1
    Why can't you just add Project1 and Project2 as submodules inside your project? – TamaMcGlinn Apr 02 '20 at 15:00
  • I'm not sure I understand what you mean, but if you mean moving the 3rd party stuff into my python project and then using that as the root, I would like to avoid cluttering my python project with a bunch of 3rd party stuff (in actuality there are far more than just 2 extra projects). If you mean leaving the structure as is, but adding the other projects as submodules, that seems to have the same problems described in the OP (i.e. it will result in a single-folder public repo and it will require special handling when cloning the private repo). – Mate de Vita Apr 03 '20 at 09:44
  • The original problem is 'However, if I want to push this to a public repository, I cannot include the 3rd party projects. So I have to push my project in a different state to the public repository.' You go on to state the solution: 'submodules are intended for use when you want to independently develop a part of your repository' If Project1 and Project2 are submodules, then their code is not included in your project repository. – TamaMcGlinn Apr 03 '20 at 13:08
  • Only the fourth requirement, that you don't want to have to do `git submodule update --init` after cloning, throws a wrench in the works. It's a bit of a bizarre requirement though. Perhaps you could work around it with a githook that issues that command upon cloning? – TamaMcGlinn Apr 09 '20 at 05:49

0 Answers0