如果这是您的防火墙阻止 git: 协议端口 (9418) 的问题,那么您应该进行更持久的更改,这样您就不必记住为每个 git repo 发出其他帖子建议的命令。
以下解决方案也仅适用于可能也使用 git: 协议的子模块。
由于 git 消息并没有真正立即指向防火墙阻塞端口 9418,让我们尝试将其诊断为实际问题。
诊断问题
参考:https://superuser.com/q/621870/203918 和 https://unix.stackexchange.com/q/11756/57414
我们可以使用多种工具来确定是否是防火墙导致了我们的问题 - 使用您系统上安装的任何工具。
# Using nmap
# A state of "filtered" against port 9418 (git) means
# that traffic is being filtered by a firewall
$ nmap github.com -p http,git
Starting Nmap 5.21 ( http://nmap.org ) at 2015-01-21 10:55 ACDT
Nmap scan report for github.com (192.30.252.131)
Host is up (0.24s latency).
PORT STATE SERVICE
80/tcp open http
9418/tcp filtered git
# Using Netcat:
# Returns 0 if the git protocol port IS NOT blocked
# Returns 1 if the git protocol port IS blocked
$ nc github.com 9418 < /dev/null; echo $?
1
# Using CURL
# Returns an exit code of (7) if the git protocol port IS blocked
# Returns no output if the git protocol port IS NOT blocked
$ curl http://github.com:9418
curl: (7) couldn't connect to host
好的,现在我们已经确定是我们的 git 端口被防火墙阻止了,我们能做些什么呢?继续阅读:)
基本 URL 重写
Git 提供了一种使用 git config 重写 URL 的方法。只需发出以下命令:
git config --global url."https://".insteadOf git://
现在,就像变魔术一样,所有 git 命令都会将git:// 替换为https://
这个命令做了什么改变?
使用以下命令查看您的全局配置:
git config --list
您将在输出中看到以下行:
url.https://.insteadof=git://
您可以通过查看~/.gitconfig 来查看文件中的内容,您现在应该会看到已添加以下两行:
[url "https://"]
insteadOf = git://
想要更多控制权?
只需在替换中使用更完整/更具体的 URL。例如,要仅让 GitHub URL 使用 https:// 而不是 git://,您可以使用如下内容:
git config --global url."https://github".insteadOf git://github
您可以使用不同的替换多次运行此命令。但是,如果 URL 匹配多个替换,则最长匹配“获胜”。每个 URL 只会进行一次替换。
系统管理员的系统范围变化
如果您是 Linux 系统管理员并且您不希望您的用户必须经历上述痛苦,您可以快速更改系统范围的 git 配置。
只需编辑或添加以下内容到/etc/gitconfig,您的用户就不必担心以上任何问题:
[url "https://"]
insteadOf = git://