Gitの警告"You have divergent branches and need to specify how to reconcile them."は、pull時分岐したブランチに対する設定を明確にすることで解決する。

特定のブランチに対し、gitとgithubのデータを同期したい。

でもどっちが先になっているのか、時間が経って覚えてない...

まずは、リモートブランチをfetchして様子を見てみる。

git fetch -u origin feature/contents-front

>>
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 5 (delta 3), reused 5 (delta 3), pack-reused 0
Unpacking objects: 100% (5/5), 447 bytes | 223.00 KiB/s, done.
From https://github.com/ventus-inc/Orical_admin2
 * branch              feature/contents-front -> FETCH_HEAD
   c9e6b87a..fddf6379  feature/contents-front -> origin/feature/contents-front

どうやら無事に済んだ様子。

次にローカルブランチへmergeしてみる。

git merge feature/contents-front

>>
Already up to date.

すでにアップデート済みとのこと。

では、今のローカルリポジトリの内容を、リモートブランチにpushしてみる。

git push origin feature/contents-front:feature/contents-front

>>
To https://github.com/ventus-inc/Orical_admin2.git
 ! [rejected]          feature/contents-front -> feature/contents-front (non-fast-forward)
error: failed to push some refs to 'https://github.com/ventus-inc/Orical_admin2.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.

ダメらしい...

branchを再度確認したり、statusを再確認したり、もう一度mergeしてみたり...でも問題なし...なぜ?...

試しにpullしてみる。

git pull origin feature/contents-front

>>
From https://github.com/ventus-inc/Orical_admin2
 * branch              feature/contents-front -> 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の警告"You have divergent branches and need to specify how to reconcile them."は、pull時分岐したブランチに対する設定を明確にすることで解決する。

どうやら、

"[Git]の[pull]を行なった際の[警告]"You have divergent [branches] and need to specify how to reconcile them."は、[pull]時[分岐]した[ブランチ]に対する設定([config])を明確にすることで解決する。"

という考えが必要らしい。

ヒントは以下。

  • 分岐しっ放しの複数の[ブランチ]があるため、それらに対してどう操作(調節)を行うか、挙動を明確にしなければなりません。
  • [pull]する前に以下の[コマンド]を実行すれば、この設定を行うことができます。
  • git config pull.rebase false # [merge]で取り込む。

    (デフォルト)

  • git config pull.rebase true # [rebase]で取り込む。

  • git config pull.ff only # [fast-forward]で取り込む。
  • これらのコマンドに--global[オプション]をつけておけば、全[リポジトリ]のデフォルト設定として設定できます。
  • また、pullするたびに--rebase, --no-rebase, --ff-onlyと[オプション]をつければ、デフォルト設定の挙動とはまた違う挙動でpullを行うことができます。

なぜ?

  • 2020/6/1のGit 2.27.0から発生するようになった警告
  • これらの三種類の挙動をユーザーに区別させることで混乱を減らす目的。

解決方法

  • [config]を用いて、[pull.rebase], [pull.ff]のいずれかの設定を明確化する。
  • pullをする毎にオプションをちゃんと指定する。

で、やったこと。

まずは、再度pullする前に以下のコマンドを実行。

git config pull.rebase false

怖いので、もう一度pull。

git pull origin feature/contents-front

>>
From https://github.com/ventus-inc/Orical_admin2
 * branch              feature/contents-front -> FETCH_HEAD
Merge made by the 'ort' strategy.

まだ怖いので、もう一度pull。

git pull origin feature/contents-front

>>
From https://github.com/ventus-inc/Orical_admin2
 * branch              feature/contents-front -> FETCH_HEAD
Already up to date.

Alreadyと出るくらいだから流石にもういいだろうと、pushしてみる。

git push origin feature/contents-front:feature/contents-front

>>
Enumerating objects: 2, done.
Counting objects: 100% (2/2), done.
Delta compression using up to 10 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 382 bytes | 382.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/ventus-inc/Orical_admin2.git
   fddf6379..882457af  feature/contents-front -> feature/contents-front

おお!治った!!