【Git】Git初心者がローカルの既設プロジェクトをGitHubと連携させようとしてハマった話

#はじめに
pushとかpullとか基本は理解できたし、いろいろ実践してみよう！
→まずはローカルの既設プロジェクトをGitHubと連携させよう！
→しょっぱなからハマりました...。

以下にやったこととエラー内容を備忘録として雑記。（また整理します。）

#経緯
1.GitHubでリポジトリ作成
「Initialize this repository with a README」にチェック
 　→これが諸悪の根元。

2.ローカルの既設プロジェクトがあるディレクトリに移動してgit init

```
$ cd [既設プロジェクトがあるディレクトリ]
$ git init --bare --share
$ git remote add origin https://github.com/[ユーザーID]/[リポジトリ名].git
```
3.add → commit → push （エラー発生）

```
$ git add -A
$ git commit -m "first commit"
$ git push origin master
```
以下、出たエラー内容。

```
To https://github.com/[ユーザーID]/[リポジトリ名].git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/[ユーザーID]/[リポジトリ名].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.
```

#すべきだったこと

```
$ git reset --merge
$ git merge --allow-unrelated-histories origin/master
$ git push origin master
```

#やったこと

```
$ git pull origin master

warning: no common commits
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/yubitani/SelfTP
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
fatal: refusing to merge unrelated histories


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

hint: Waiting for your editor to close the file... error: There was a problem with the editor 'vi'.
Not committing merge; use 'git commit' to complete the merge.


$ git merge --allow-unrelated-histories origin/master -m "first commit"

fatal: You have not concluded your merge (MERGE_HEAD exists).
Please, commit your changes before you merge.


$ git status

On branch master
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:

        new file:   README.md

$ git push origin master

To https://github.com/yubitani/SelfTP.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/yubitani/SelfTP.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.


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

fatal: You have not concluded your merge (MERGE_HEAD exists).
Please, commit your changes before you merge.


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

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


$ git push origin master

Enumerating objects: 80, done.
Counting objects: 100% (80/80), done.
Delta compression using up to 4 threads
Compressing objects: 100% (72/72), done.
Writing objects: 100% (79/79), 27.68 KiB | 1.84 MiB/s, done.
Total 79 (delta 9), reused 0 (delta 0)
remote: Resolving deltas: 100% (9/9), done.
To https://github.com/[ユーザーID]/[リポジトリ名].git
   81672f2..8f9a238  master -> master
```