首页 > 解决方案 > 我该如何处理这个 Git 警告?“不鼓励在不指定如何协调不同分支的情况下进行拉取”

问题描述

之后git pull origin master我收到以下消息:

warning: Pulling without specifying how to reconcile divergent branches is
discouraged. You can squelch this message by running one of the following
commands sometime before your next pull:

  git config pull.rebase false  # merge (the default strategy)
  git config pull.rebase true   # rebase
  git config pull.ff only       # fast-forward only

You can replace "git config" with "git config --global" to set a default
preference for all repositories. You can also pass --rebase, --no-rebase,
or --ff-only on the command line to override the configured default per
invocation.

remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), 51.49 KiB | 850.00 KiB/s, done.

然后拉取成功。但是,我仍然对这条消息表示怀疑。

在这种情况下,最好的办法是什么?

标签: git

解决方案


在其默认模式下,git pull 是 git fetch 后跟 git merge FETCH_HEAD 的简写。

当您执行 a 时git pull origin master
git pull执行合并,这通常会创建合并提交。因此,默认情况下,从远程拉取并不是一个无害的操作:它可以创建一个以前不存在的新提交 SHA 哈希值。这种行为可能会使用户感到困惑,因为感觉应该是无害的下载操作实际上会以不可预知的方式改变提交历史。

为避免这种情况,您需要

git pull --ff-only

(或不?继续阅读,看看哪一个符合您的要求)

使用git pull --ff-only,Git 将仅在可以“快速转发”而不创建新提交的情况下更新您的分支。如果这不能完成,git pull --ff-only只需中止并显示错误消息。

您可以将 Git 客户端配置为始终--ff-only默认使用,因此即使您忘记了命令行标志,您也会得到此行为:

git config --global pull.ff only

注意:该--global标志将更改应用于您机器上的所有存储库。如果您只希望您所在的存储库具有此行为,请省略该标志。

取自这里



此警告是在 Git 2.27 中添加的。

这是完整警告的样子:

不鼓励在不指定如何协调不同分支的情况下进行拉取。您可以通过在下次拉取之前的某个时间运行以下命令之一来消除此消息:

git config pull.rebase false # 合并(默认策略)
git config pull.rebase true # rebase
git config pull.ff only # fast-forward only

您可以将“git config”替换为“git config --global”来为所有存储库设置默认首选项。您还可以在命令行上传递 --rebase、--no-rebase 或 --ff-only 以覆盖每次调用的配置默认值。

警告显示三个命令作为选项,所有这些都将抑制警告。但它们有不同的用途:

git config pull.rebase false     # merge (the default strategy)

这将保留默认行为并抑制警告。

git config pull.rebase true      # rebase

这实际上是在远程分支之上提交,在本地和远程都维护一个分支(与涉及两个不同分支的默认行为不同 - 一个在本地,另一个在远程 - 并且,将两者结合起来,执行合并)。

git config pull.ff only          # fast-forward only

仅当本地分支可以快进时才执行拉取。如果没有,它会简单地中止并显示错误消息(并且不会创建任何提交)。


更新:

如果您有 Git 2.29 或更高版本,您现在可以设置pull.fffalsetrueonly以摆脱警告。

git config pull.ff true

true- 这是默认行为。如果可能,拉取快进,否则合并。

git config pull.ff false

false- 拉取永远不会快进,并且总是会创建合并。

git config pull.ff only

only- 如果可能,拉动将快进,否则操作将中止并显示错误消息。


注意:您可能需要在此处关注VonC的回答,以获取在未来更新中对此功能所做更改的更新。


推荐阅读