はじめに
はじめてpullしようとしたらタイトルの警告文が表示されpullできませんでした。
予期しないことは学びのチャンス!ということで、調べた内容と解決策を書いていこうと思います。
環境
- macOS:Ventura 13.4
- Git:2.40.1
表示メッセージ全文
➜ myproj git:(main) git pull origin main
From https://github.com/mof-51/myproj
* branch main -> FETCH_HEAD
* [new branch] main -> origin/main
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.
最終行を見るとNeed to specify how to reconcile divergent branches.
と書かれています。
分岐したブランチを調整せずにpullするのは推奨しませんとのこと。
つまりは「pullした時にローカルリポジトリのブランチに対してどうしたいのか決めてからpullして!」ということです。
なんでこんな警告文が出る?
これはGitのバージョンが2.27にアップデートされてから表示されるようになったメッセージで、GitHubの仕様変更によるものです。
"git pull" issues a warning message until the pull.rebase
configuration variable is explicitly given, which some existing
users may find annoying---those who prefer not to rebase need to
set the variable to false to squelch the warning.
「pullした時の挙動を決めるまで警告文を出し続けるよ!」と書かれています。
以前のバージョンでのgit pull
のデフォルトの挙動はこうでした。
- リモートリポジトリからデータをダウンロード
- 対象のブランチにマージ
しかし、2.27.0以降はgit pullでダウンロードしたデータをローカルリポジトリに対してどう適用するのかを選択する必要があるんです。
選択肢の違いは?
ここで警告メッセージの一部をもう一度見てみましょう。
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
コメントアウトで記載されている通りですが、それぞれの選択肢の挙動は以下になります。
-
git config pull.rebase false
:pullしてmergeする -
git config pull.rebase true
:pullしてrebaseする -
git config pull.ff only
:pullしてfast fowordする
解決策
選択肢のいずれかの表示コマンドを打てばpullできるようになります。
なお、コマンドは一度打てばgit configに設定が書き込まれるため、以降のgit pull
はすべて同じ挙動になります。
さいごに
間違いなどがありましたらぜひご指摘いただけると幸いです。
最後までお読みいただきありがとうございました!
参考
https://qiita.com/tearoom6/items/0237080aaf2ad46b1963
https://github.com/git/git/blob/master/Documentation/RelNotes/2.27.0.txt