GIT 第22章 Pulling without specifying how to reconcile divergent branches is discouraged GIT 第22章 Pulling without specifying how to reconcile divergent branches is discouraged

2023-07-25

一、问题

git pull

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.

那么 pull 就成功了。但是,我仍然对这条消息表示怀疑。在这种情况下,最好的做法是什么?

当你做 git pull origin master ,git pull 执行 merge ,这通常会创建 merge 提交。因此,默认情况下,从远程 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 config pull.rebase false

保留默认行为并抑制警告 

git config pull.rebase true

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

git config pull.ff only

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

三、更新

如果您有 Git 2.29 或以上,您现在可以设置 pull.ff 至 false , true 或 only摆脱警告。

git config pull.ff true

true - 这是默认行为。如果可能, pull 是快进的,否则它被 merge 。

git config pull.ff false

false - pull 永远不会快进,并且总是会创建 merge 。

git config pull.ff only

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

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开微信扫一扫,即可进行扫码打赏哦

阅读 320