error: failed to push some refs to 'remote'
Gitでリモートにプッシュできない場合のエラー
概要
Gitでローカルの変更をリモートリポジトリにプッシュしようとした際、リモートにローカルにない変更がある場合に発生するエラーです。
エラーメッセージ
error: failed to push some refs to 'https://github.com/user/repo.git'
hint: Updates were rejected because the remote contains work that you do not have locally.
hint: This is usually caused by another repository pushing to the same ref.
hint: You may want to first integrate the remote changes (e.g., 'git pull ...') before pushing again.
原因
- リモートに新しいコミットがある: 他の人がプッシュした
- 別のブランチからプッシュした: 同じリモートブランチに複数人がプッシュ
- GitHub上で直接編集: WebUIでファイルを編集した
- force pushの履歴不整合: 誰かがforce pushして履歴が変わった
- マージリクエスト: PRがマージされてリモートが更新された
解決策
1. pull してからpush(最も一般的)
| |
2. fetch + merge
| |
3. fetch + rebase(履歴をきれいに保つ)
| |
4. pull with rebase
| |
5. 強制プッシュ(危険)
| |
6. 新しいブランチとしてプッシュ
| |
7. 状態の確認
| |
コンフリクト解決
| |
よくある間違い
- pullせずに何度もpushを試みる
- main/masterブランチでforce pushする
- コンフリクトを解決せずにcommitしようとする
関連エラー
参考リンク
Git の他のエラー
この記事は役に立ちましたか?