fuuengineerのブログ

エンジニアになります。

error: failed to push some refs to 'URL'.gitの対処法【git】

下記のエラーは私がコミットしたローカルの変更をリモートにプッシュした際にでたエラーです。

$ git push origin main
To https:///githubのURL 
[rejected]main -> main (non-fast-forward)error: failed to push some refs to 'https://githubのURL'.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.

私が先にgithubのindex.htmlの内容を変更し、その後にコミットしたことがエラーの原因となったと考えます。 リモートとローカルの整合性が取れず、プッシュできないとのこと。

解決方法

リモートの変更をマージしてから、改めてpushする。
方法1

$ git pull origin main

方法2

$ git fetch origin main
$ git merge origin/main

最終手段

$ git push -f origin main

この-fは「強制的に」という意味。