git://协议被公司阻止,我该如何解决这个问题?

Rob*_*ert 184 git github

尝试git clone git://github.com/ry/node.git不起作用的东西会导致:

Initialized empty Git repository in /home/robert/node/.git/
github.com[0: 207.97.227.239]: errno=Connection timed out
fatal: unable to connect a socket (Connection timed out)
Run Code Online (Sandbox Code Playgroud)

但是,通过HTTP克隆工作正常.到目前为止,我已经收集到这是协议的问题,但我正在尝试安装需要命令的cloud9

git submodule update --init --recursive

它试图使用git://协议并失败.有没有办法改变命令的工作方式?

Nat*_*igh 422

如果这是防火墙阻止git:protocol端口(9418)的问题,那么您应该进行更持久的更改,这样您就不必记住为每个git repo发出其他帖子建议的命令.

以下解决方案也适用于可能也使用git:协议的子模块.

由于git消息并未真正指向防火墙阻塞端口9418,因此我们尝试将其诊断为实际问题.

诊断问题

参考文献:https://superuser.com/q/621870/203918https://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
Run Code Online (Sandbox Code Playgroud)

好的,现在我们已经确定我们的git端口被防火墙阻止了,我们能做些什么呢?继续阅读:)

基本URL重写

Git提供了一种使用重写URL的方法git config.只需发出以下命令:

git config --global url."https://".insteadOf git://
Run Code Online (Sandbox Code Playgroud)

现在,好像通过魔术,所有git命令将执行替换git://https://

这个命令做了什么改变?

使用以下方法查看您的全局配置:

git config --list
Run Code Online (Sandbox Code Playgroud)

您将在输出中看到以下行:

url.https://.insteadof=git://
Run Code Online (Sandbox Code Playgroud)

您可以通过查看~/.gitconfig现在应该看到添加以下两行的位置来查看文件的外观:

[url "https://"]
    insteadOf = git://
Run Code Online (Sandbox Code Playgroud)

想要更多控制?

只需在替换中使用更完整/特定的URL.例如,要仅使GitHub URL使用https://而不是git://,您可以使用以下内容:

git config --global url."https://github".insteadOf git://github
Run Code Online (Sandbox Code Playgroud)

您可以使用不同的替换多次运行此命令.但是,如果URL匹配多个替换,则最长匹配"获胜".每个URL只能进行一次替换.

系统管理员的系统范围更改

如果您是Linux系统管理员并且不希望用户必须经历上述困难,那么您可以在系统范围内快速进行git配置更改.

只需编辑或添加以下内容即可/etc/gitconfig,您的用户无需担心以上任何内容:

[url "https://"]
    insteadOf = git://
Run Code Online (Sandbox Code Playgroud)

  • 这个就像一个魅力.非常感谢! (8认同)
  • 简约明快! (8认同)
  • 要更多地控制转换哪个URL,您也可以指定URL的一部分.例如:我有一个私有内部服务器'myserver.lan.example.com',它通过SSH(gitlab)而不是HTTPS来托管git repos.因此,如果我想利用方便的密钥身份验证,我必须使用SSH.我也使用来自Github的repos,但是我的公司防火墙阻止了SSH到Github.我不想简单地用'https://'替换'git://'的所有实例,因为这会破坏gitlab.解决方案是`git config --global url."https:// github".insteadOf git:// github`. (7认同)
  • 要撤消此更改,可以使用`git config --global --unset url."https://".insteadOf` (3认同)
  • 我是从cygwin里面运行git的,唯一可以让它工作的方法就是对系统管理员进行"系统范围的更改"并添加'url.https://.insteadof=git://'更改为'C:\ Program Files(x86)\ Git\etc\gitconfig'文件.谢谢你的暗示! (2认同)

Cas*_*bel 29

Github也提供http(s)访问权限,这不太可能被贵公司阻止.要告诉子模块使用它,您可以这样做:

git submodule init
git config submodule.<name>.url https://github.com/...
git submodule update
Run Code Online (Sandbox Code Playgroud)

这实际上就是为什么init和update是单独的命令 - 你可以初始化,自定义位置,然后更新.update --init只是您不需要自定义任何URL的快捷方式.

对于发生这种情况的其他任何人,您当然也可以使用ssh URL(如果您的公司阻止git://但不阻止ssh),但在这种情况下,OP可能没有SSH访问远程仓库.

  • @Robert:如果有很多,你可以直接编辑配置文件并进行搜索并替换:`sed -i的@git:// github @ https:// github @'.git/config`. (4认同)

elp*_*dev 13

另一个不涉及触摸git配置的选项是将ssh设置更改为使用端口443而不是常规22端口.

参考:通过HTTPS端口使用SSH

从那篇文章:

edit the file at ~/.ssh/config, and add this section:

Host github.com
   Hostname ssh.github.com   
   Port 443
Run Code Online (Sandbox Code Playgroud)

之后,我能够成功地向Github推送.在家里,你可以改变ssh配置,如果你想要的话.


K M*_*lam 6

我有一段时间也遇到了同样的问题.然后我尝试使用建议的命令更改git配置:

git config --global url."https://".insteadOf git://
Run Code Online (Sandbox Code Playgroud)

不幸的是,这对我来说并没有成功.我还有同样的问题!

实际上最终解决了我的问题的是,我使用以下命令再次重置了我的存储库的远程URL:

git remote set-url origin https://github.com/<my_user_name>/<my_repo_name>.git
Run Code Online (Sandbox Code Playgroud)

以前是这样的:

git remote set-url origin git@github.com:<my_user_name>/<my_repo_name>.git
Run Code Online (Sandbox Code Playgroud)

设置远程网址后使用https://而不是git@git.com问题解决了我.

  • 我遇到了类似的问题。似乎设置全局仅影响未来克隆的存储库,并且不会追溯任何更改。 (2认同)