概要
git push heroku masterコマンドを入力して、Herokuにデプロイをしようとしたときに起きました。
% git push heroku master
To https://git.heroku.com/~~.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://git.heroku.com/~~.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.
ポイントは rejected, non-fast-forward, hint。
結論
結論から言うとHerokuとローカルのcommitの差分(状態)があっていなかったため起きたエラーでした。Herokuの方がcommitが進んでいたようです。
解決策
解決策は以下のどちらかが考えられました!簡単にいうと、git pull heroku masterはHerokuの状態をローカルに合わせる、git pull origin masterはリモートの状態をローカルに反映させてリモートの状態とローカルの状態を合わるということだと思います。
要するに、リモート、ローカル、Heroku(デプロイ後)の状態を最新にしておく、ということです。
今回はgit pull heroku masterで解決しました!
% git pull origin master
% git pull heroku master
仮説
まずこれがどういう状況のエラーなのか。rejectedというのはデプロイを拒否されている。丁寧にhintをだしてくれているので、ひとつひとつ理解していけば解いていけそうですね。また、non-fast-forwardはすでにpushされているからpushできないよ、というエラー文ですね。
基本的にmasterブランチからデプロイをしていたのでgit commit --allow-empty -m "空のcommit"をしてからgit push heroku masterをしていました。空のcommitをしたあとにUndoやRevertをしてしまってその状態で新しく作業ブランチを作ってしまっていのかもしれません。。
またHerokuのログを見てエラー文を見るというのも有効だと思います。
% heroku logs --tail --app アプリ名
hintの一文目に注目すると
Updates were rejected because the tip of your current branch is behind
直訳すると***現在のブランチの先端が後ろにあるため、更新が拒否されました。***という意味です。commitのズレ、差分が生じているということですかね。。
git push heroku masterでエラーが起きたので考えたのは次の2つ。
1.リモートとローカルに差分がでている。
2.Herokuとローカルに差分がでている。
検証
1.リモートとローカルに差分がでている。
% git pull origin master
warning: Pulling without specifying how to reconcile divergent branches is
discouraged. You can squelch this message by running one of the following
commands sometime before your next pull:
git config pull.rebase false # merge (the default strategy)
git config pull.rebase true # rebase
git config pull.ff only # fast-forward only
You can replace "git config" with "git config --global" to set a default
preference for all repositories. You can also pass --rebase, --no-rebase,
or --ff-only on the command line to override the configured default per
invocation.
From https://github.com/~~
* branch master -> FETCH_HEAD
Already up to date.
Already up to date!ということはすでに最新の状態になっている。(もしくは、最新にしてくれたということかも)
その後git push heroku masterをしてみたが特に変わりなく同じエラーがでたので次。
2.Herokuとローカルに差分がでている。
% git pull heroku master
warning: Pulling without specifying how to reconcile divergent branches is
discouraged. You can squelch this message by running one of the following
commands sometime before your next pull:
git config pull.rebase false # merge (the default strategy)
git config pull.rebase true # rebase
git config pull.ff only # fast-forward only
You can replace "git config" with "git config --global" to set a default
preference for all repositories. You can also pass --rebase, --no-rebase,
or --ff-only on the command line to override the configured default per
invocation.
From https://git.heroku.com/~~
* branch master -> FETCH_HEAD
Already up to date!
Merge made by the 'recursive' strategy.
ポイントだと思うところを下記に記します。
Already up to date!
Merge made by the 'recursive' strategy.
自動的にmergeしてくれたようなので、GitHub Desktopを確認しました!
過去のcommitがmergeされて差分がなくなったようです。
確認後、git push heroku masterでデプロイ完了しました。
まとめ
- ローカルとリモートのリポジトリの状態を最新にしておき、その後デプロイを行う。
- UndoやRevertなどは便利な機能ですが使うときはどうなるのか考えてから行う。
- エラー文にhintがあるときはそれを読み解いていくと解決がはやくなり、次にエラーが起きても応用が効くかもしれない
下記記事参考にさせていただきました!ありがとうございました!!
http://shoprev.hatenablog.com/entry/2014/07/31/200540
https://qiita.com/taka_no_okapi/items/0444afde4817920d0671
https://qiita.com/nasutaro211/items/c590994a5d5091206c08