将 git 配置为默认使用 IPv4 而不是 IPv6

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21671706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 09:42:47  来源:igfitidea点击:

Configure git to use IPv4 instead of IPv6 by default

gitipv6ipv4

提问by nobeh

Checking the environment variables and also HTTP configuration options does not reveal something. Is there a way to do this?

检查环境变量和 HTTP 配置选项并没有发现任何东西。有没有办法做到这一点?

采纳答案by VonC

With git 2.8 (March 2016), you can force git fetch/push/cloneto use IPV4 or IPV6.
(for git pull, see below Git 2.16, Q1 2018)

使用Git 2.8(2016年3月),你可以强制git fetcH / push/clone使用IPv4或IPv6。
(对于git pull,请参见下面的 Git 2.16,2018 年第一季度)

See commit c915f11(03 Feb 2016) by Eric Wong (ele828).
(Merged by Junio C Hamano -- gitster--in commit e84d5e9, 24 Feb 2016)

请参阅Eric Wong ( )提交的 c915f11(2016 年 2 月 3 日)(由Junio C Hamano合并-- --提交 e84d5e9 中,2016 年 2 月 24 日)ele828
gitster

connect& http: support -4and -6switches for remote operations

Sometimes it is necessary to force IPv4-only or IPv6-only operation on networks where name lookups may return a non-routable address and stall remote operations.

-4, --ipv4:

Use IPv4 addresses only, ignoring IPv6 addresses.

-6; --ipv6:

Use IPv6 addresses only, ignoring IPv4 addresses.

connect& http:远程操作的支持-4-6开关

有时需要在名称查找可能返回不可路由地址并停止远程操作的网络上强制执行 IPv4-only 或 IPv6-only 操作。

-4, --ipv4:

仅使用 IPv4 地址,忽略 IPv6 地址。

-6; --ipv6:

仅使用 IPv6 地址,忽略 IPv4 地址。



Update Git 2.16 (Q1 2018): Contrary to the documentation, "git pull -4/-6 other-args" did not ask the underlying "git fetch" to go over IPv4/IPv6, which has been corrected.

更新 Git 2.16(2018 年第一季度):与文档相反,“ git pull -4/-6 other-args”没有要求底层“ git fetch”通过 IPv4/IPv6,这已得到纠正。

See commit ffb4568(19 Nov 2017) by Shuyu Wei (``).
(Merged by Junio C Hamano -- gitster--in commit c2b6135, 27 Nov 2017)

请参阅Shuyu Wei (``)提交的 ffb4568(2017 年 11 月 19 日)
(由Junio C gitsterHamano合并-- --提交 c2b6135 中,2017 年 11 月 27 日)

回答by Anthony C Howe

The more generic solution (that also works for BSD) is to edit the global /etc/ssh/ssh_configor per-user ~/.ssh/configand add/replace the entry:

更通用的解决方案(也适用于 BSD)是编辑全局 /etc/ssh/ssh_config或每个用户~/.ssh/config并添加/替换条目:

AddressFamily any 

with

AddressFamily inet

You can also set this for just a single host:

您还可以仅为单个主机设置此项:

Host example.com
    AddressFamily inet

回答by Jeremy Visser

In 99% of cases, you should notbe doing this. The real answer to the question is fix your IPv6 connection.

在 99% 的情况下,您应该这样做。这个问题的真正答案是修复您的 IPv6 连接

Failing that, you can edit /etc/gai.confto prefer IPv4 over IPv6. gai.conf modifies the behaviour of getaddrinfo(), which almost all IPv6–supporting applications use to resolve hostnames.

如果失败,您可以编辑/etc/gai.conf以更喜欢 IPv4 而不是 IPv6。gai.conf 修改了 的行为getaddrinfo(),几乎所有支持 IPv6 的应用程序都使用它来解析主机名。

Almost all systems ship with a copy of /etc/gai.conf within their glibcor libcpackage. If it is missing from /etc, usually I find a template copy is lurking somewhere within /usr/share/doc, for you to copy into /etc and modify accordingly.

几乎所有系统都在其glibclibc包中附带了 /etc/gai.conf 的副本。如果 /etc 中缺少它,通常我会发现模板副本潜伏在 /usr/share/doc 中的某处,供您复制到 /etc 中并进行相应修改。

回答by fcm

There are situations where upgrade git or where IPV6 is running but not working as expected, for example, under Windows Linux subsystem, my solutions is simple: just add the git/httpb/ssh IPV4 location on /etc/hosts.

在某些情况下,升级 git 或 IPV6 正在运行但未按预期工作,例如,在 Windows Linux 子系统下,我的解决方案很简单:只需在 /etc/hosts 上添加 git/httpb/ssh IPV4 位置。

Use hostto get the IPV4 address and append it to /etc/hosts with name for your own use, for example, 1.1.1.1 git4.server.com

使用host获得的IPv4地址和其追加到/ etc /与主机名称供自己使用,例如,1.1.1.1 git4.server.com

回答by Rodrigo Estevao

For those that are using Ubuntu/Debian, the best way to have the latest git version is following the steps below, taken from here.

对于那些使用 Ubuntu/Debian 的人来说,获得最新 git 版本的最佳方法是按照以下步骤操作,取自此处

sudo add-apt-repository ppa:git-core/ppa  
sudo apt-get update  
sudo apt-get install git  

Updating for a version 2.19+ plus the solution above, proposed by Anthony C Howe has solved my problem.

更新版本 2.19+ 加上上面由 Anthony C Howe 提出的解决方案解决了我的问题。