【Git】git pushしたらrejectされた時のエラー解決方法！non-fast-forwardやgit configも対応！

# 概要
* git pushしたら以下エラーと遭遇したので、その対応方法を記しておく。
    * ` ! [rejected] ...error: failed to push some refs to...`
    * `Updates were rejected because the remote contains work that you do...`
    * `! [rejected] ...(non-fast-forward)`
    * `You have divergent branches and need to specify how to reconcile them`

# エラー内容
* いつものように`git add .`と`git commit`を実施してから`git push origin　xxx`したら、以下エラーメッセージが返された。

```terminal
To xxxxxxxxx.git
 ! [rejected]        feature/1 -> feature/1 (fetch first)
error: failed to push some refs to 'xxxxxxxxx.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.
```

* rejectじゃと？！
* 「前回と今回の push までの間にリモートが更新されているので、あなたのローカルに存在しないファイルがある」という意味らしいので、リモートファイルの取得とマージを行なってみよう
    * 参考記事：[git push が 「the remote contains work that you do hint: not have locally」でrejected された時の対処方法](https://pgmemo.tokyo/data/archives/1535.html)


* `git fetch`を実施

```terminal
remote: Enumerating objects: 353, done.
remote: Counting objects: 100% (353/353), done.
remote: Compressing objects: 100% (128/128), done.
remote: Total 353 (delta 190), reused 316 (delta 166), pack-reused 0
Receiving objects: 100% (353/353), 103.22 KiB | 308.00 KiB/s, done.
Resolving deltas: 100% (190/190), completed with 11 local objects.
From xxxxxx...
```

* `git merge`を実施

```terminal
fatal: No remote for the current branch.
```

* あれ、、だめだ、リモートにないと言われてる

* `git push`を実施してみると、
```terminal
fatal: The current branch feature/1 has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin feature/1

To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.
```

* 上記のようにかえってきたので、`git push --set-upstream origin feature/1`を実施
```terminal
To xxxxxxxxx.git
 ! [rejected]        feature/1 -> feature/1 (non-fast-forward)
error: failed to push some refs to 'xxxxxxxxx.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.
```

* (non-fast-forward)という、微妙に違うエラーになった。下記を参考に`git pull origin feature/1`を実施
    * 参考記事：[non-fast-forward エラーの扱い](https://docs.github.com/ja/get-started/using-git/dealing-with-non-fast-forward-errors)

```terminal
From xxxxxxxxx
 * branch            feature/1 -> FETCH_HEAD
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint: 
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.
```

* 今度はまた別のエラーだ(^^;)
* git pull時に、分岐したブランチに対する設定を促す警告らしい。
    * ３つのうちいずれかやっておきなさい、とのことなので、`git config pull.rebase false`をまずは実施
    * 参考記事：[Gitの警告"You have divergent branches and need to specify how to reconcile them."は、pull時分岐したブランチに対する設定を明確にすることで解決する。](https://scrapbox.io/appbirdNotebook-public/Git%E3%81%AE%E8%AD%A6%E5%91%8A%22You_have_divergent_branches_and_need_to_specify_how_to_reconcile_them.%22%E3%81%AF%E3%80%81pull%E6%99%82%E5%88%86%E5%B2%90%E3%81%97%E3%81%9F%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81%E3%81%AB%E5%AF%BE%E3%81%99%E3%82%8B%E8%A8%AD%E5%AE%9A%E3%82%92%E6%98%8E%E7%A2%BA%E3%81%AB%E3%81%99%E3%82%8B%E3%81%93%E3%81%A8%E3%81%A7%E8%A7%A3%E6%B1%BA%E3%81%99%E3%82%8B%E3%80%82)

* `git pull origin feature/1`を実施すると、pullできたぞ！

```terminal
From xxxxxxxxx
 * branch            feature/1 -> FETCH_HEAD
Merge made by the 'ort' strategy.
 #####
 #省略
```

* `git push origin feature/1`を行うと、無事にpush完了！よしゃあー！！！！

```terminal
Enumerating objects: 38, done.
Counting objects: 100% (33/33), done.
Delta compression using up to 10 threads
Compressing objects: 100% (24/24), done.
Writing objects: 100% (26/26), 6.85 KiB | 3.43 MiB/s, done.
Total 26 (delta 11), reused 5 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (11/11), completed with 4 local objects.
To xxxxxxxxx.git
   01988a0..2568093  feature/1 -> feature/1
```

* まだまだ学ぶことが多い日々です！