Using a single git repository for multiple git projects

前端 未结 5 685
一向
一向 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: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.

提交回复
热议问题