Webシステム開発の雑多なアウトプット

AWS、プログラム、OSS等を中心に良かった本も。

【git】「git push」で「rejectedエラー」(fetch first)

「git push」したところエラーが発生したので対処方法をメモっておきます。

rejectedエラー発生(fetch first)

git pushをしたところ、! [rejected] master -> master (fetch first)のエラー発生。

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


原因

Githubに直接アップロード(コミット)したものがあったようで(記憶になし、、)
まずはローカルにマージしろ、というエラー。


対処方法

①フェッチ

git fetchを実行します。
$ git fetch
remote: Enumerating objects: 18, done.
remote: Counting objects: 100% (18/18), done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 16 (delta 8), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (16/16), done.
From https://github.com/xxxxxx/yyyyyy.git
811d7b0..7d133c6 master -> origin/master

②マージ

git merge origin/masterを実行します。もしもコンフリクトした場合、競合ファイルを保存や削除します。
$ git merge origin/master
Merge made by the 'recursive' strategy.
.../aaa.txt" | 467 +
...
12 files changed, 93728 insertions(+)

③プッシュ -> 成功!

最後に、git pushを実行します。
$ git push origin master
Enumerating objects: 14, done.
Counting objects: 100% (14/14), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (10/10), 1.65 MiB | 267.00 KiB/s, done.
Total 10 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To https://github.com/xxxxxx/yyyyyy.git
7d133c6..ca6595f master -> master