bower list 命令有问题,这是由于 bower 使用 git 和 git:// URL 来获取远程 GitHub 存储库列表,但 git:// 协议被我们的公司防火墙阻止。为了解决这个问题,除了设置环境变量外,我还得给 git 添加额外的配置。这是我必须执行的命令的完整列表(记得用你的替换代理主机和端口):
# set proxy for command line tools
export HTTP_PROXY=http://localhost:3128
export HTTPS_PROXY=http://localhost:3128
export http_proxy=http://localhost:3128
export https_proxy=http://localhost:3128
# add configuration to git command line tool
git config --global http.proxy http://localhost:3128
git config --global https.proxy http://localhost:3128
git config --global url."http://".insteadOf git://
Bash 中的标准环境变量是大写的,对于代理,它们是 HTTP_PROXY 和 HTTPS_PROXY,但有些工具希望它们是小写的,bower 就是其中之一。这就是为什么我更喜欢在两种情况下设置代理:低位和高位。
Bower 使用 git 从 GitHub 获取包,这就是为什么配置键也需要添加到 git 的原因。 http.proxy 和 https.proxy 是代理设置,应该指向您的代理。最后但同样重要的是,您需要告诉 git 不要使用git:// 协议,因为它可能被防火墙阻止。您需要将其替换为标准的http:// 协议。有人建议使用https:// 而不是git://,如下所示:git config --global url."https://".insteadOf git://,但我收到Connection reset by peer 错误,所以我使用http://,这对我来说很好。
在家里我不使用任何代理,也没有公司防火墙,所以我更喜欢切换回“正常”的无代理设置。这是我的做法:
# remove proxy environment variables
unset HTTP_PROXY
unset HTTPS_PROXY
unset http_proxy
unset https_proxy
# remove git configurations
git config --global --unset http.proxy
git config --global --unset https.proxy
git config --global --unset url."http://".insteadOf
我不太擅长记住事情,所以我永远不会记住所有这些命令。除此之外,我很懒,不想手动输入那些长命令。这就是为什么我要创建函数来设置和取消设置代理设置。这是我在一些别名定义之后添加到我的.bashrc 文件中的 2 个函数:
set_proxy() {
export HTTP_PROXY=http://localhost:3128
export HTTPS_PROXY=http://localhost:3128
# some tools uses lowercase env variables
export http_proxy=http://localhost:3128
export https_proxy=http://localhost:3128
# config git
git config --global http.proxy http://localhost:3128
git config --global https.proxy http://localhost:3128
git config --global url."http://".insteadOf git://
}
unset_proxy() {
unset HTTP_PROXY
unset HTTPS_PROXY
unset http_proxy
unset https_proxy
git config --global --unset http.proxy
git config --global --unset https.proxy
git config --global --unset url."http://".insteadOf
}
现在当我需要设置代理时,我只需执行set_proxy 命令,并取消设置unset_proxy 命令。在 Bash 的自动完成功能的帮助下,我什至不需要输入这些命令,而是让 tab 帮我完成它们。