ITのえんぴつ

Google Blockly開発者ツールやIT・プログラミングを研究して発信するブログ

【GitHub】push時にrejected[拒否]されたときの対処法!

事象

git push しようとしたら、以下のエラーが表示されました。

To git@github.com:companyx/projectx.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:companyx/projectx.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.

原因

対象ブランチのリモートとローカルの最新情報が異なっているためです。

こんなときに発生する

  • 別の人が同じブランチにすでにプッシュした
  • すでにコミット状態の中、ローカルのコードを新たに変更してpushした

など

対処法

① git pull コマンドを実行して、ローカルの対象ブランチを最新にする

リモート → ローカル

git pull origin ブランチ名

② git push コマンドを実行して、リモートの対象ブランチへ反映する

リモート ← ローカル

git push origin ブランチ名

参考