如何将文件夹与现有的Heroku应用程序链接

877

我在GitHub上有一个现有的Rails应用程序,并已部署在Heroku上。我正在尝试设置一个新的开发机器,并从我的GitHub存储库克隆了该项目。然而,我对如何将此文件夹链接到Heroku感到困惑。最初,我使用了heroku create命令,但显然这次我不想这样做,因为它会创建另一个Heroku实例。

9个回答

1334

Heroku会将你的项目基于heroku git远程链接在一起(还有其他一些选项,请参见下面的更新)。要将Heroku远程作为当前存储库中的远程添加,请使用以下命令:

git remote add heroku git@heroku.com:project.git

在这里,project 代表你的 Heroku 项目的名称(与 project.heroku.com 子域相同)。完成后,你可以使用 heroku xxxx 命令(假设你已经安装了 Heroku Toolbelt),并通过 git push heroku master 像往常一样将代码推送到 Heroku。如果你正在使用命令行工具的话,可以使用以下快捷方式:

heroku git:remote -a project

在这里,project 是你的 Heroku 项目名称(感谢Colonel Panic)。你可以通过传递 -r remote_name 来任意命名 Git 远程库的名称。

[更新]

正如 Ben 在评论中提到的那样,远程库的名称不需要为 heroku 才能运行 gem 命令。我查看了源代码,它似乎是这样工作的:

  1. 如果您通过 --app 选项指定了应用名称(例如 heroku info --app myapp),它将使用该应用。
  2. 如果您通过 --remote 选项指定了 Git 远程库的名称(例如 heroku info --remote production),它将使用与该 Git 远程库相关联的应用。
  3. 如果您没有指定任何选项,并且您的 Git 配置文件中设置了 heroku.remote,它将使用与该远程库相关联的应用(例如,要将默认远程库设置为“production”,请在存储库中使用 git config heroku.remote production 命令,并且 Heroku 将运行 git config heroku.remote 以读取该设置的值)。
  4. 如果您没有指定任何选项,而且 gem 在您的 .git/config 文件中找不到配置,且 gem 在您的 Git 远程库中只找到一个 URL 中包含 "heroku.com" 的远程库,则它将使用该远程库。
  5. 如果这些都不起作用,则会引发错误提示您向命令传递 --app

1
如果您尝试使用heroku git:remote方法,请检查您是否正在使用heroku toolbelt cli,并且您的系统上没有安装任何heroku gem(似乎在我使用旧的gems时无法正常工作)。 - Pierre-Adrien
3
另外,如果您有多个Heroku分支,可以使用“$ git config heroku.remote <appname>”来设置默认应用程序,以便您可以在终端为该应用程序使用命令。 - Sheharyar
我使用第一种方法时遇到了权限错误,但第二种方法可以正常工作。heroku git:remote -a project - pansay
1
如果使用http,例如heroku login,则远程的格式为https://git.heroku.com/project-name.git - max pleaner
2
注意:Heroku现在在项目概述中描述了它:导航到https://dashboard.heroku.com/,单击您的应用程序,然后切换到“部署”选项卡并选择部署方法“Heroku git”(默认选中)。然后,您将获得有关如何将现有文件夹/git与Heroku连接的说明。 - handy
显示剩余4条评论

250

1
! Resource not found - Green
2
通过传递 -r other_remote_name,您可以将远程命名为任何您想要的名称。 - MasterScrat
2
即使您已将Heroku应用程序直接连接到GitHub,这仍然是正确的方法。请注意,它不会覆盖您的“origin”远程;而是创建一个名为“heroku”的远程。 - Seth
我是Heroku的新手,它正在显示.. 将git远程设置为https://git.heroku.com/my-service.git..现在该怎么办? - Sunil Garg

147

不要忘记,如果你也在一台你之前没有设置Heroku的机器上

heroku keys:add

否则你将无法向仓库推送或拉取。


4
哦,是的!这对我也有帮助。我有一个应用程序,我们从基础开始,然后将“heroku”分为“staging”和“production”。所以我做了git remote add staging git@staging.xx:yy.gitgit remote add production git@production.xx:yy.git--但这还不够。当我尝试执行git push staging master(或production)时,我会收到“Permission denied(publickey)。fatal:The remote end hung up unexpectedly”的错误提示。解决这个错误的方法是像Ghoti提到的那样执行heroku keys:add - Purplejacket

15
heroku login 

git init

heroku git:remote -a app-name123

然后检查远程仓库:

git remote -v

14

为旧应用程序设置新部署系统时需要注意两个事项

1.检查您的应用程序是否可以访问Heroku(特别是该应用程序)。

heroku apps

它将列出您可以访问的应用程序,如果您首次设置,可能需要

heroku keys:add

2. 然后设置你的git远程

如果已经创建了Heroku应用程序,您可以使用heroku git:remote命令轻松地向本地存储库添加远程。您所需要的只是Heroku应用程序的名称:

heroku git:remote -a appName

你也可以使用 git remote rename 命令来重命名远程仓库:

git remote rename heroku heroku-dev(you desired app name)

然后,您可以使用git remote命令确认是否已为您的应用设置了远程仓库

 git remote -v

4
使用Heroku的分支。
  1. Use the new "heroku fork" command! It will copy all the environment and you have to update the github repo after!

    heroku fork -a sourceapp targetapp
    
  2. Clone it local

    git clone git@heroku.com:youamazingapp.git
    
  3. Make a new repo on github and add it

    git remote add origin https://github.com/yourname/your_repo.git
    
  4. Push on github

    git push origin master
    

2

1

0

对于现有的代码库

在终端中输入以下命令

$ heroku git:remote -a example

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接