Gitで発生したエラー【Updates were rejected because the tip of your current branch is behind】

gitで初歩的なエラー

 $(master) git push origin master
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/xxxxxxxxxxx/Vue-website.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

原因としてあげられるのは、手動でリポジトリを作成したからです。
手動でリポジトリを作成した場合、README.txtが生成されますが、
リモート先のリポジトリ(README.txt)とローカルリポジトリに齟齬が生じているという話。

解決策

単純にgit pull origin masterしてやればいいです。

ただし・・・

$(master) git pull origin master
 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

エラーが出ます。
原因はローカルmasterブランチとリモートmasterブランチの履歴に関連性が無いからだろうと推測しています。

qiita.com

'git pull'は'git fetch'+'git merge origin/master'をまとめてやってくれます。
'git merge origin/master'で履歴に関連性のないブランチをmergeするには、
以下のコマンドを実行しないといけないようです。

git merge --allow-unrelated-histories origin/master

ローカルリポジトリにリモートリポジトリの内容(今回はREADME.txtのみ)がmergeされます。

$(master) git merge --allow-unrelated-histories origin/master
Merge made by the 'recursive' strategy.
 README.md | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 README.md

完了!

$(master) git push origin master
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 322 bytes | 322.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/xxxxxxxxxxxx/Vue-website.git
   f85824e..3aa151e  master -> master