GitHubのpush時にエラー発生
git push origin masterと入力し、GitHubに変更内容を反映させようとしたところ
下記のようなエラーが発生して怒られてしまいました。
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/MASAKi-cell/php01.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.
解決方法
調べてみると、stackoverflowで同じようにエラーに直面している方がいました。
Issue pushing new code in GitHubリモートリポジトリを作成した際、Readme.mdを作成しており、ローカルリポジトリでは
その内容がcommitしてないので、反映させる必要がある様です。
git fetchコマンドでリモートから最新情報をローカルに持ってくる
リモートリポジトリの内容がローカルに反映されていないので「fetch」コマンドを使用して、リモートリポジトリの最新情報を取得していきます。
さらに「merge」してmasterへの最新情報を持ってくるようにします。
git fetchの詳細は下記Qiita記事を参照してください。
【初心者向け】git fetch、git merge、git pullの違いについて
$ git fetch origin master
$ git merge origin/master
これで、ローカルにReadme.mdが反映されるので、再度「git push origin master」と打ち込むと正常に反映されます。
以上です。