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

この記事からわかること

  • GitHubrejectedされた時の解決法
  • rejected[拒否]される原因

index

[open]

\ アプリをリリースしました /

みんなの誕生日

友達や家族の誕生日をメモ!通知も届く-みんなの誕生日-

posted withアプリーチ

今回は以下のようなエラーが表示されたときの解決法をまとめていきます。

$ git push origin main
〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜
! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/users/repository.git'

今回はブランチ名をBLM運動への配慮からmasterではなくmainに変更しています。詳細や伴うエラーは下記記事を参考にしてください。

pushがrejected(拒否)される原因

ローカル環境(ローカルリポジトリ)で開発後GitHub(リモートリポジトリ)にアップ(push)する際にエラーが発生しました。

$ git push origin main
〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜
! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/users/repository.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.

これを日本語に訳すと以下のような意味になります。

! [拒否]メイン->メイン(最初にフェッチ)
エラー:一部の参照を「https://github.com/users/repository.git」にプッシュできませんでした
ヒント:リモートにあなたが行った作業が含まれているため、更新は拒否されました
ヒント:ローカルにはありません。これは通常、別のリポジトリがプッシュすることによって発生します
ヒント:同じ参照に。最初にリモートの変更を統合することをお勧めします
ヒント:(例: 'git pull ...')もう一度押す前に。
ヒント:詳細については、「gitpush--help」の「早送りに関する注意」を参照してください。

このエラーが発生する原因は別のローカルリポジトリからプッシュしたことです。

つまり現在のローカルリポジトリの中のpush済みのファイルとリモートリポジトリの中のファイルに齟齬があり、一致していないのが原因です。

GitHubにpush時にrejectedが起きる原因

私の場合はローカル環境(MAMP)ローカルリポジトリを作成後、pushしGitHubにアップ、それを本番環境(サーバー内)にpullしていました。しかしGitHubにアップできるファイルの制限や小さな変更をGitHubを介さずFTPで送信したり、本番環境のローカルリポジトリからpushしたりしてしまいました。

この状況を生み出す可能性

初心者ならではのミスだと思います(反省)。

rejectedされるのを解決する方法

この状況を解決するのは簡単で「現在のローカルリポジトリに内容をリモートリポジトリの内容と結合(マージ)」すればOKです。

pullコマンドはブランチをマージする機能も持っているので以下のコマンドを実行すれば解決できました。

$ git pull origin main
〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜
remote: Enumerating objects: 17, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 7 (delta 5), reused 7 (delta 5), pack-reused 0
Unpacking objects: 100% (7/7), 631 bytes | 63.00 KiB/s, done.
From URL
 * branch            main       -> FETCH_HEAD
                     main       -> origin/main
hint: Pulling without specifying how to reconcile divergent branches is
hint: discouraged. You can squelch this message by running one of the following
hint: commands sometime before your next pull:
hint: 
hint:   git config pull.rebase false  # merge (the default strategy)
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
Already up to date.
Merge made by the 'recursive' strategy.

まだまだ勉強中ですので間違っている点や至らぬ点がありましたら教えていただけると助かります。

ご覧いただきありがとうございました。

searchbox

スポンサー

ProFile

ame

趣味:読書,プログラミング学習,サイト制作,ブログ

IT嫌いを克服するためにITパスを取得しようと勉強してからサイト制作が趣味に変わりました笑
今はCMSを使わずこのサイトを完全自作でサイト運営中〜

New Article

index