69

所以我知道 heroku 用作 git 存储库,但假设我想使用 github 以及存储库。如何设置它以使我有两个存储库并且两者都同步?

4

6 回答 6

99

您可以在 git 安装上拥有多个遥控器。你会有一个 github 遥控器和一个 heroku 遥控器。

假设您已经设置了 github,那么您可能会通过以下方式推送到 github:

git push origin master

origin是你的遥控器,master是你的分支。

按照开始使用 Heroku的说明选择您想要的语言并继续完成教程。本教程假设您已经安装了 github,并将向您展示如何创建您的heroku远程通过heroku create.

然后你像往常一样推送到 github,并通过以下方式推送到 heroku:

git push heroku master

相同的格式适用 -heroku是您的遥控器,master也是您的分支。您没有在此处覆盖您的 Github 远程,而是添加了另一个,因此您仍然可以通过一个提交使用工作流程进行两次推送,例如:

git add .
git commit -m "Going to push to Heroku and Git"
git push origin master -- push to Github Master branch
git push heroku master -- push to Heroku
于 2013-03-05T19:05:27.990 回答
37

If you want to be able to push and pull to multiple remotes:

First add them:

git remote add origin <github repo>
git remote add heroku git@heroku.com:<app name>.git

Then push

git push origin master
git push heroku master

If you want to push to both remotes at the same time:

Edit you configuration file so origin points to both heroku and github:

git config -e

Add/Replace:

[remote "origin"]
    url = git@github.com:username/somerepo.git
    url = ssh://git@bitbucket.org/username/somerepo.git

Since you are using github you can integrate with heroku by navigating to:

https://dashboard.heroku.com/apps/<app name>/settings#github-repo

and adding your repository's name.

github integration

If you want to automatically push to heroku after committing to GitHub:

you will need to use a continuous integration platform like TravisCI.

Here are the steps to make this work. Be careful what you push to production, make sure it works before it gets deployed. Each method has its pros and cons.

于 2014-04-02T21:38:39.450 回答
23

我认为这实际上是推荐的情况;Heroku git 存储库功能实际上是用于部署而不是代码管理。

只需像往常一样使用 github 管理您的代码,但在准备好部署时另外推送到 Heroku git 存储库。无需将它们与自动化工具等保持同步,因为您希望能够在不部署的情况下推送到您的 github 存储库,例如,以便您可以备份或协作处理未完成的功能或维护单独的暂存和生产环境.

于 2013-03-05T19:04:29.443 回答
17

我经常这样做。我为 Heroku 创建了一个站点,但我想将我的源代码保存在 Github 中以用于存档目的。我设置为遥控器:

git remote add origin <github repo>

git remote add heroku <heroku repo>

git push origin master然后你就可以了git push heroku master。Heroku 还允许您关联 github 存储库以查看提交差异。

于 2013-03-05T19:07:27.893 回答
12

因为之前没有人提到过。Git 现在允许您向每个遥控器添加多个 url。这样做:

这将添加fetchpush来自github:

git remote add origin git@github.com:yourName/yourGithubRepo.git

github push这将覆盖heroku push

git remote set-url origin --push --add git@heroku.com:yourHerokuRepo.git

这将重新添加github push

git remote set-url origin --push --add git@github.com:yourName/yourGithubRepo.git

这是最终的输出:

$ git remote -v
origin  git@github.com:yourName/yourGithubRepo.git (fetch)
origin  git@heroku.com:yourHerokuRepo.git (push)
origin  git@github.com:yourName/yourGithubRepo.git (push)

之后运行:

git push

如果它不是在工作,而是在说关于设置上游,那么首先输入:

git push --set-upstream origin master 
于 2014-05-17T14:08:23.587 回答
2

如果您不想管理两个存储库并且只管理 Github 上的一个,那么您可以这样做(假设您已经创建了一个 Heroku 应用程序)。

1) 首先,将 Heroku 存储库克隆到本地。

2)然后创建一个 Github repo 并将这个本地推送到那里。

3) 完成后,使用wercker

4) 转到“添加应用程序”并填写详细信息。它们相当简单。使用您刚刚创建的 Github 存储库。

5)添加应用程序后转到设置并添加部署目标。从列表中选择 heroku。然后选择您之前创建的 Heroku 应用程序和您想要推送的分支。

而已!你完成了。现在您的 Github 存储库已与您的 Heroku 应用程序同步。您使用的任何推送到 Github 存储库的内容

git push origin master

将自动部署到您的 Heroku 应用程序。这样,您就可以在 Github 上管理您的存储库,并且您只需处理一个存储库。:)

于 2014-06-05T19:49:58.397 回答