git pushエラー:error: failed to push some refs to…はリモートの方が新しいのでプッシュできないという意味

プログラミング関連

対処法

git fetch

してリモートの最新状態をローカルに取得して

git merge

で取得した最新情報のファイルをローカルに統合する。

この後、エラーが起きた push コマンドを再度たたけば問題なくローカル/リモートでファイルを最新状態にできる。

わたしの場合は、一度pushしたリポジトリにGitHub上でREADME.mdファイルを作成後に再度pushしようとした事が原因だった。

実際のエラーと対処の様子

わたしの場合、このエラーが出る前に、一度ローカルのファイルをpush して、GitHubの方でREADME.mdファイルを編集していた。(要するに GitHub上にはローカルにはないREADME.mdファイルが新規作成されている状態)
その後、別のソースファイルを git add, git commit後、git push -u originなどとプッシュしようとしてこのエラーが出た。

実際のエラーメッセージ

$ git push -u origin title_to_game
To github.com:mrgarita/PocketShooting0.git
 ! [rejected]        title_to_game -> title_to_game (fetch first)
error: failed to push some refs to 'github.com:mrgarita/PocketShooting0.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.

上記エラーメッセージだと2行目に fetch first とある。
「まずはfetchしなよ」みたいな感じか?

対処時の様子

この後、git fetch するとこうなった。

$ git fetch
remote: Enumerating objects: 116, done.
remote: Counting objects: 100% (116/116), done.
remote: Compressing objects: 100% (109/109), done.
remote: Total 109 (delta 61), reused 0 (delta 0), pack-reused 0R
Receiving objects: 100% (109/109), 30.06 KiB | 267.00 KiB/s, done.
Resolving deltas: 100% (61/61), completed with 1 local object.
From github.com:mrgarita/PocketShooting0
   00c9e9f..09c4700  title_to_game     -> origin/title_to_game
   fc71c79..754d4ba  boss_appearance   -> origin/boss_appearance
   9ea2ee5..8e4c8df  boss_bullet1      -> origin/boss_bullet1
   343fa8d..3afa13d  boss_bullet2      -> origin/boss_bullet2
   09bcddf..1b25651  boss_motion       -> origin/boss_motion
   5b562fd..603bfee  game_message      -> origin/game_message
   8bc84a8..e144467  player_kya_effect -> origin/player_kya_effect

次に git merge する

$ git merge
Merge made by the 'recursive' strategy.
 README.md | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)
 create mode 100644 README.md

これでGitHub上で更新したREADME.mdがローカルに取り込まれた。

最後にエラーが出たpushコマンドを実行したら問題なくプッシュされた。

$ git push -u origin title_to_game
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 1.11 KiB | 1.11 MiB/s, done.
Total 5 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To github.com:mrgarita/PocketShooting0.git
   09c4700..43e317c  title_to_game -> title_to_game
Branch 'title_to_game' set up to track remote branch 'title_to_game' from 'origin'.

参考にしたサイト

【初心者向け】git fetch、git merge、git pullの違いについて - Qiita
gitで手こずった時に色々ググってると、「git fetch」と「git pull」がぐちゃぐちゃになってしまったのでま...

コメント

タイトルとURLをコピーしました