Git将Rails推送到Heroku时出现错误

5
当我尝试运行git push heroku master命令时,出现以下错误:
matt@matt-desktop:~/Documents/Ruby/rails_projects/ninja_speak_app$ git push heroku master
To https://git.heroku.com/limitless-inlet-4477.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://git.heroku.com/limitless-inlet-4477.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

这个仓库是昨天在Github上创建的,名为ninja_speak_app。在设置仓库时,我小心地没有初始化自述文件或许可证。

当我执行git pull命令时,会得到以下结果:

matt@matt-desktop:~/Documents/Ruby/rails_projects/ninja_speak_app$ git pull
Already up-to-date.

提交操作还显示所有内容已经是最新的:

matt@matt-desktop:~/Documents/Ruby/rails_projects/ninja_speak_app$ git commit -m "first commit"
On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working directory clean

编辑:运行 git push --force heroku master 后会出现以下情况:

matt@matt-desktop:~/Documents/Ruby/rails_projects/ninja_speak_app$ git push --force heroku master
Counting objects: 25701, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (25297/25297), done.
Writing objects: 100% (25701/25701), 104.98 MiB | 97.00 KiB/s, done.
Total 25701 (delta 322), reused 25668 (delta 309)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: Build stream timed out, reverting to polling....
remote: !   Timeout reached polling for results.
remote: 
To https://git.heroku.com/limitless-inlet-4477.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/limitless-inlet-4477.git'

我能够使用"--force"将代码上传至Heroku,但为什么仍然出现错误?
编辑2:
运行"git pull heroku master"命令会产生以下结果:
matt@matt-desktop:~/Documents/Ruby/rails_projects/ninja_speak_app$ git pull heroku master
warning: no common commits
remote: Counting objects: 69191, done.
remote: Compressing objects: 100% (45212/45212), done.
remote: Total 69191 (delta 17653), reused 68275 (delta 16751)
Receiving objects: 100% (69191/69191), 129.00 MiB | 884.00 KiB/s, done.
Resolving deltas: 100% (17653/17653), done.
From https://git.heroku.com/limitless-inlet-4477
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> heroku/master
Auto packing the repository for optimum performance. You may also
run "git gc" manually. See "git help gc" for more information.
Counting objects: 673, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (594/594), done.
Writing objects: 100% (673/673), done.
Total 673 (delta 239), reused 0 (delta 0)
error: The following untracked working tree files would be overwritten by merge:
    db/development.sqlite3
    db/production.sqlite3
    db/test.sqlite3
    log/development.log
    log/production.log
    log/test.log
    tmp/cache/assets/C12/AE0/sprockets%2Fc558458ba9671201141a50f2240184c9
    ...
    ...
Aborting

执行 git push heroku master 后,会出现以下结果:

matt@matt-desktop:~/Documents/Ruby/rails_projects/ninja_speak_app$ git push heroku master
To https://git.heroku.com/limitless-inlet-4477.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://git.heroku.com/limitless-inlet-4477.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

你的 git pull 操作被中止了。请先提交你的工作再进行拉取操作,或者放弃更改,或者使用 stash 命令。 - Andrew C
5个回答

5
如果您确定自己在做什么,可以强制将推送到Heroku:
git push --force heroku master

有没有不使用--force参数的方法来完成这个操作? - mtcrts70
在我看来,你已经在本地回滚了一个提交,然后将其推送到了 Github,因此你的 Heroku 存储库和本地/Github 存储库不同步。为了解决这个问题,请在 Heroku 上进行强制推送。 - user4384619

5

这意味着您的代码库中有比本地代码库更新的代码。 您需要在本地合并您的更改:

git pull heroku master

然后您需要推送:
git push heroku master

编辑(按照您的编辑):

尝试使用git fetch origin

git pull --rebase

再次推送


编辑了问题并附上了尝试后产生的消息。有什么想法吗? - mtcrts70

1
这是最终适用于我的方法:
我登录了我的heroku帐户并转到“部署”选项卡。从这里,有一个“连接到GitHub”的选项,它将允许您手动链接两个帐户。链接帐户后,您可以在heroku帐户上搜索GitHub存储库。链接后,我选择了“手动部署”选项。自从进行了这个手动部署以来,我已经能够使用git push heroku master而没有任何问题。

我将测试这个版本,因为回滚到以前的版本没有起作用。 - nxmohamad

0

你需要运行

git pull heroku_app_name master

希望它能够正常工作。

matt@matt-desktop:~/Documents/Ruby/rails_projects/ninja_speak_app$ git pull limitless-inlet-4477 master 致命错误:'limitless-inlet-4477'似乎不是git存储库 致命错误:无法从远程存储库读取。请确保您拥有正确的访问权限并且存储库存在。 - mtcrts70
你需要执行以下操作:git remote add heroku git@heroku.com:project.git,其中的git url可以在你的Heroku应用设置页面中找到。希望现在能够正常工作。 - sansarp
运行了以下命令:git remote add heroku git@heroku.com:limitless-inlet-4477.git,但是当运行git push heroku master时,仍然出现与问题描述相同的错误(即“无法推送一些引用”)。 - mtcrts70

0
如果您使用了以下命令: - bundle install --without production 并且没有执行以下命令: - git commit -am "installed using bundle"
那么您更有可能遇到这样的问题。
如果您已经运行了以下命令: -bundle install--without production 那么请按照以下步骤操作,即可解决问题:
  1. 打开您的Gemfile文件,并在其中添加您的Ruby版本:
    • ruby '2.2.1'
  2. $ git commit -am "updated the Gemfile"
  3. $ git push heroku master
这将解决该问题。

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