对多个 git 项目使用单个 git 存储库

发布于 2024-12-10 10:04:38 字数 1073 浏览 0 评论 0原文

我很便宜。我不想为很多 github 帐户付费。

我有一个看起来像这样的项目结构。

  • /repo 是项目根目录。
  • /repo/project1 是我有一个 Java 项目的地方
  • /repo/herokurails1 是我有一个 Ruby/Rails 项目的地方,该项目部署到 heroku.com (到一个名为 Heroku 应用程序) hr1-fubar.heroku.com)
  • /repo/herokurails2 是我有另一个 Ruby/Rails 项目的地方,它部署到 heroku.com (一个名为 Heroku 应用程序) hr2-fubar.heroku.com)。

所有项目(project1、herokurails1 和 herokurails2 正在积极开发中)。

我希望在某个 git 托管位置(例如 github.com)或什至在办公室的本地 git 服务器上拥有一个存储库。每当 /repo/ 中的任何地方代码发生更改时,我希望能够运行“git push github master”并拥有整个 /repo/树被推送到 github。

但是,我还希望能够将我的 Rails 应用程序部署到 heroku.com,而无需将整个 repo/* 树推送到每个 heroku 应用程序。例如

  • /repo/herokurails1> git push heroku master 只会将herokurails1项目部署到heroku,而不会在/repo/*
  • /repo/herokurails2>中推送任何其他代码。 git push heroku master 只会将herokurails2项目部署到heroku,也不会推送/repo/*中的任何其他代码。

这可能吗?我需要设置什么特别的东西才能实现这一点吗?或者我是否被多个 git 存储库困住了?

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 project root.
  • /repo/project1 is where I have a Java project
  • /repo/herokurails1 is where I have a Ruby/Rails project which deploys to heroku.com (to a heroku app called hr1-fubar.heroku.com)
  • /repo/herokurails2 is where I have another Ruby/Rails project which deploys to heroku.com (to a heroku app called hr2-fubar.heroku.com).

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.

  • /repo/herokurails1> git push heroku master would only deploy the herokurails1 project to heroku without pushing up any other code in /repo/*
  • /repo/herokurails2> git push heroku master would only deploy the herokurails2 project to heroku also without pushing up any other code in /repo/*.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦幻的味道 2024-12-17 10:04:38

这很快就会变得非常笨重(特别是 Git 不会让你像 svn 那样推拉子目录)

我会考虑不使用 Github 来存储项目。 BitbucketCodebaseHQ 有不同的定价模型,这些模型可能更适合您并为您提供所需的简单性。

或者,看看像 Gitosis 这样的东西,它可以通过 Gitlab 等工具变得非常像 Github 风格

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

老街孤人 2024-12-17 10:04:38

您可以在本地使用 git subtree维护一个从较大的 repo 存储库中分离出来的 git 存储库,并从该分离的存储库中进行推送。不过,坦率地说,我要么使用 Adam Dymitruk 建议 或者只是向 GitHub 多付一点钱 - 毕竟他们确实提供了优质的服务......

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

初懵 2024-12-17 10:04:38

如果您愿意,如果您不想支付任何费用,可以使用 unfuddle 或 bitbucket。使用子模块在 github 上托管大型文件,例如图像、第 3 方 dll、视频等。这些不会拥有IP。

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.

一指流沙 2024-12-17 10:04:38

将每个单独的项目放在不同的分支中怎么样?到目前为止,它对我来说效果很好。我有一个 webProjects 的私人仓库,每个分支都以 master 开头,其中有我的 web 项目模板(也可以是空分支),每次我有新项目时,我只是创建一个基于 master 的分支,其中包含项目名称并保持正常工作。

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.

全部不再 2024-12-17 10:04:38

这个问题已经没有意义了,因为 GitHub 现在允许用户免费拥有无限的存储库,但无论如何我都会分享我的知识,因为我认为有人会觉得这很有趣。

问题是,您可以拥有一个包含多个项目的存储库,而无需使用 git 子模块创建任何其他存储库。通常,为了使用 git 子模块,您必须为每个子模块创建一个存储库,但我意识到,您可以重用要包含在存储库中的每个项目,而不是为每个项目创建一个单独的存储库。项目,如下所示:

  1. 创建将包含 GitHub 上多个项目的存储库。
    为了这个答案,假设这个存储库位于 https://github.com/example/example .git

  2. 在本地克隆该存储库:

    git 克隆 https://github.com/example/example.git
    
  3. 为您想要拥有的每个项目创建一个(最好是孤立的)分支:

    git checkout --orphan project_1/master
    git commit -m “project_1/master 的第一次提交”
    git push --set-upstream origin project_1/master
    
    git checkout --orphan project_2/master
    git commit -m“project_2/master 的第一次提交”
    git push --set-upstream origin project_2/master
    
    git checkout --orphan project_3/master
    git commit -m“project_3/master 的第一次提交”
    git push --set-upstream origin project_3/master
    
  4. 在分支 master 中为每个项目添加一个子模块指定正确的分支

    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 子模块添加 --branch project_3/master -- https://github.com/example/example.git project_3
    
  5. 提交并推送:

    git commit -m“已将 3 个项目添加到存储库”
    git 推送
    

现在您可以使用此存储库中的每个项目,就好像它们位于自己的存储库中一样。但是,每次创建新分支时,都必须在项目的分支文件夹下创建它。例如,如果您想在project_1中创建一个分支“new_feature”,您必须执行以下命令:

git branch 'project_1/new_feature'

如果您想在其他地方下载项目,您可以:

  • 克隆项目本身而不是其他内容:

     git clone --branch project_1 https://github.com/example/example.git ./project_1
    
  • 如果您想同时使用多个项目,则克隆整个存储库,然后仅下载您实际要使用的项目的内容:

     git 克隆 https://github.com/example/example.git
     git 子模块 init 项目_1 项目_3
     git 子模块更新 --remote
    

如果您总是使用 --remote 进行 git submodule update,您可能还想在 git 时执行此命令以停止显示项目名称来自主存储库的状态

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

优点:

  • 每次想要使用另一个项目时,您不必检出另一个分支
  • 由于您没有切换分支,因此您也不必处理未跟踪的文件
  • 您可以有效地使用只有一个存储库。

缺点:

  • 您必须注意在正确的文件夹中创建每个分支。
    您创建一个名为 project_1/feature 的分支,而不是 feature

替代方案:

  • git subtree:我知道这个工具是为此特定目的而创建的,但是它有一些缺点,我不知道如何使用它。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文