Jay Godse
Jay Godse

Reputation: 15533

Using a single git repository for multiple git projects

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

I have a project structure which looks something like this.

All projects (project1, herokurails1, and herokurails2 are under active development).

I would like to have one repository at some git hosting place such as github.com or even a local git server in the office. Whenever code changes anywhere in /repo/, I want to be able to run "git push github master" and have my entire /repo/ tree pushed up to github.

However, I would also like to be able to deploy my rails apps to heroku.com without pushing the entire repo/* tree pushed to each heroku app. e.g.

Is this possible? Do I need to set up anything special to make this happen? Or am I stuck with multiple git repositories?

Upvotes: 10

Views: 7083

Answers (5)

Adrian
Adrian

Reputation: 2001

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.

Upvotes: 0

Yas
Yas

Reputation: 1

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.

Upvotes: 0

Adam Dymitruk
Adam Dymitruk

Reputation: 129762

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.

Upvotes: 1

Neil Middleton
Neil Middleton

Reputation: 22240

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

Upvotes: 5

Mark Longair
Mark Longair

Reputation: 468081

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...

Upvotes: 2

Related Questions