如何处理此git警告&引用;不鼓励在不指定如何协调分歧分支的情况下拉取“;

如何处理此git警告&引用;不鼓励在不指定如何协调分歧分支的情况下拉取“;,git,Git,在git拉取原始主机后我收到以下消息: 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

git拉取原始主机后
我收到以下消息:

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 config pull.ff only
或等效的
git pull--ff only
是最安全的。原因是,如果另一个开发人员已强制推送到同一分支,则重新基础可能会覆盖历史记录,并可能导致提交丢失

但它们都是有效的

在默认模式下,git pull是git fetch的缩写,后跟git merge fetch\u HEAD

当您执行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#仅快进

您可以将“git config”替换为“git config--global”,以设置默认值 所有存储库的首选项。你也可以传递-再基,-不再基, 或者--ff仅在命令行上覆盖配置的默认值 调用

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

git config pull.rebase false     # merge (the default strategy)
git config pull.rebase true      # rebase
git config pull.ff only          # fast-forward 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 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.ff
设置为
false
true
only
,以消除警告

git config pull.ff true
true
-这是默认行为。如果可能的话,Pull是快进的,否则它会被合并

git config pull.ff false
false
-拉从不快进,并且始终创建合并

git config pull.ff only
-如果可能,Pull是快进的,否则操作将中止并显示错误消息。

这是以下内容中的新警告:


要删除警告,如果未在命令行上指定行为(使用
--ff
--no ff
--ff
--ff only
--ff
--rebase
),请将其中一个建议值设置为git pull的首选默认行为。在所有情况下,
git
都将尝试快进()合并(如果可能)。这些设置控制分支中有更改但远程分支中不存在更改时发生的情况

  git config pull.rebase false  # merge (the default strategy)
这是现有的默认行为;将此设置为无警告和行为无变化
git
将远程分支合并到本地分支

  git config pull.rebase true   # rebase
在这里,
git
将尝试在远程分支上重新设置更改的基础。请参阅,以了解有关您可能需要该选项的原因的更多详细信息

  git config pull.ff only       # fast-forward only
如果无法进行快进合并,
git
将拒绝继续。引述如下:

拒绝合并并以非零状态退出,除非当前标头已经是最新的或合并可以作为快进解决

注意:前面我们教过“”()在用户不说需要合并历史记录、重新设置历史记录或只接受快进时发出警告,但警告是针对设置了
pull.ff
配置变量的用户触发的

Git 2.29(2020年第4季度)不再如此(意思是:不再发出警告)

参见。
(于2020年9月29日合并)

:如果设置了
pull.ff
,则不发出警告 签字人:Alex Henrie

能够充分理解设置
pull.ff
的用户不需要额外的说明


在Git 2.31(2021年第1季度)之前,当用户没有告诉“”()使用重设基础或合并时,该命令会发出一条响亮的消息,告诉用户在重设基础或合并之间进行选择,但无论如何都会创建一个合并,迫使想要重设基础的用户重做该操作

通过收紧给出消息的条件来修复此问题的早期部分——如果历史快速向前,则没有理由停止或强制用户在重设基础或合并之间进行选择

参见(2020年12月14日)by.
参见(2020年12月12日)by.