Pythonとサラリーマンと

2020年6月にPythonを始めたサラリーマンのブログです。

Herokuにpush済みのmasterを過去バージョンに強制的に戻す方法

Rails Tutorialを進行中。 本番環境の修正を行いたくてmasterへpush、pushとした結果、グチャグチャになって過去に戻りたくなった。

ローカルリポジトリでは以下を実行すると強制的にコミット履歴をなかったことにできる。

git reset [(必要なら)オプション] [ハッシュ値]

ただし、herokuに一度pushが完了すると、その時点より過去のバージョンのpushはリジェクトされてしまう。

git push heroku master
To https://git.heroku.com/xxxx.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://git.heroku.com/xxx.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.

non-fast-forwardと言われているのが、
「今pushしようとしたmasterはHerokuが今masterとして持っているバージョンよりも古いよ」
という意味っぽい。
複数人で開発しているときに、他の人がpushしたのを気づかずに上書きしてしまうのを避けるための仕様とのこと。

しかし、今回は他の人のことは考えずに、過去バージョンに戻したい。
そんな時は

git push heroku master --force

これで強制的にローカルリポジトリでmaster状態のバージョンをHerokuにpushできる。