Using a single git repository for multiple git projects

前端 未结 5 671
一向
一向 2021-02-14 11:45

I\'m cheap. I don\'t want to pay for many github accounts.

I have a project structure which looks something like this.

  • /repo is the pro
相关标签:
5条回答
  • 2021-02-14 12:17

    You could use git subtree locally to maintain a git repository which is split from your larger repo repository, and push from that split repository. Frankly, though, I'd either use one of the free services that Adam Dymitruk suggests or just pay GitHub a bit more - they do provide an excellent service, after all...

    0 讨论(0)
  • 2021-02-14 12:19

    This question does not make sense anymore since GitHub now allows users to have unlimited repositories for free, but I will share my knowledge anyways because I think someone could find this interesting.

    The thing is that you can have a repository with multiple proyects with no need to create any other repository with git submodule. Normally, in order to use git submodule you would have to have a repository for each submodule, but I realised that instead of creating a separate repository for each project you want to include in your repository, you can reuse the repository you want to include your projects in, like this:

    1. Create your repository which will contain multiple projects on GitHub.
      For the sake of this answer, let's say this repository is at https://github.com/example/example.git

    2. Clone that repository locally:

      git clone https://github.com/example/example.git
      
    3. Create a (preferably orphan) branch for each of the projects you want to have:

      git checkout --orphan project_1/master
      git commit -m "First commit of project_1/master"
      git push --set-upstream origin project_1/master
      
      git checkout --orphan project_2/master
      git commit -m "First commit of project_2/master"
      git push --set-upstream origin project_2/master
      
      git checkout --orphan project_3/master
      git commit -m "First commit of project_3/master"
      git push --set-upstream origin project_3/master
      
    4. Add in branch master a submodule for each of the projects specifying the correct branches:

      git checkout master
      git submodule add --branch project_1/master -- https://github.com/example/example.git project_1
      git submodule add --branch project_2/master -- https://github.com/example/example.git project_2
      git submodule add --branch project_3/master -- https://github.com/example/example.git project_3
      
    5. Commit and push:

      git commit -m "Added 3 projects to the repository"
      git push
      

    Now you can use each project in this repository as if they were each in it's own repository. However, each time you create a new branch you must create it under the project's branch folder. For instance, if you want to create a branch 'new_feature' in project_1 you must execute this:

    git branch 'project_1/new_feature'
    

    And if you want to download a project anywhere else, you can either:

    • Clone the project itself and nothing else:

       git clone --branch project_1 https://github.com/example/example.git ./project_1
      
    • Or clone the whole repository if you want to work with multiple projects at the same time, and then download only the contents of the projects you are actually going to work with:

       git clone https://github.com/example/example.git
       git submodule init project_1 project_3
       git submodule update --remote
      

    If you are always going to git submodule update with --remote you might want to also execute this command to stop showing the projects's name when you git status from the main repository:

    git submodule foreach 'git -C $toplevel config --file .gitmodules submodule.$name.ignore all'
    

    Advantages:

    • You don't have to checkout another branch each time you want to work with another project
    • Since you don't have switch branches, you don't have to deal with untracked files either
    • You effectively use only one repository.

    Disadvantages:

    • You must take care of creating each branch in the correct folder.
      Instead of feature you create a branch called project_1/feature

    Alternatives:

    • git subtree: I have understood this tool is created for this specific purpose, but it has some disadvantages and I don't know how to use it.
    0 讨论(0)
  • 2021-02-14 12:23

    If you want, you could use unfuddle or bitbucket if you don't want to pay anything. Use submodules to host large files such as images, 3rd party dlls, videos, etc on github. These would hold no IP.

    0 讨论(0)
  • 2021-02-14 12:26

    This is going to get very unwieldy very quickly (especially as Git won't let you push pull sub directories like svn will)

    I would look at not using Github for storing projects. Tools such as Bitbucket or CodebaseHQ have different pricing models which may well fit you better and give you the simplicity you need.

    Alternatively, look at something like Gitosis, which can be made to be very Github-esque via tools like Gitlab

    0 讨论(0)
  • 2021-02-14 12:36

    how about putting every separate project in a different branch ? it worked fine with me so far. I have a private repo for webProjects and every branch starts with master which has my web projects template (could be empty branch as well) and every time I have new project I just create a master based branch with the project name and cary on working fine.

    0 讨论(0)
提交回复
热议问题