使用Git,是否有一种方法告诉它接受自签名证书?

我使用https服务器托管git服务器,但目前证书是自签名的。

当我第一次尝试在那里创建回购时:

git push origin master -f

我得到了错误:

error: Cannot access URL     
https://the server/git.aspx/PocketReferences/, return code 22

fatal: git-http-push failed

当前回答

我的回答可能晚了,但对我有用。它可能会帮助某些人。

我尝试了上面提到的步骤,但没有解决问题。

试试这个git配置——global http。sslVerify假

其他回答

在使用sslKey或sslCert使用一行程序时要小心,如Josh Peak的回答所示:

git clone -c http.sslCAPath="/path/to/selfCA" \
  -c http.sslCAInfo="/path/to/selfCA/self-signed-certificate.crt" \
  -c http.sslVerify=1 \
  -c http.sslCert="/path/to/privatekey/myprivatecert.pem" \
  -c http.sslCertPasswordProtected=0 \
https://mygit.server.com/projects/myproject.git myproject

只有Git 2.14.x/2.15(2015年Q3)能够正确地解释~username/mykey这样的路径(同时它仍然可以解释/path/to/privatekey这样的绝对路径)。

参见Junio C Hamano (gitster)提交的8d15496(2017年7月20日)。 资助人:查尔斯·贝利(hashpling)。 (由Junio C Hamano合并- gitster -在commit 17b1e1d, 2017年8月11日)

http.c: http.sslcert and http.sslkey are both pathnames Back when the modern http_options() codepath was created to parse various http.* options at 29508e1 ("Isolate shared HTTP request functionality", 2005-11-18, Git 0.99.9k), and then later was corrected for interation between the multiple configuration files in 7059cd9 ("http_init(): Fix config file parsing", 2009-03-09, Git 1.6.3-rc0), we parsed configuration variables like http.sslkey, http.sslcert as plain vanilla strings, because git_config_pathname() that understands "~[username]/" prefix did not exist. Later, we converted some of them (namely, http.sslCAPath and http.sslCAInfo) to use the function, and added variables like http.cookeyFile http.pinnedpubkey to use the function from the beginning. Because of that, these variables all understand "~[username]/" prefix. Make the remaining two variables, http.sslcert and http.sslkey, also aware of the convention, as they are both clearly pathnames to files.

我是这样做的:

git init
git config --global http.sslVerify false
git clone https://myurl/myrepo.git

关于http。sslCAPath选项:如果在包含证书文件的目录上执行了OpenSSL c_rehash命令,git将只检测指定目录路径下的证书文件。c_rehash命令将为每个证书创建符号链接,其中链接的名称为哈希值。例如:

$ cd /path/to/ssl/cert/directory

$ ls -al

  total 16
  drwxr-xr-x  3 user  staff    96 Oct 20 13:47 .
  drwxr-xr-x  4 user  staff   128 Oct 20 13:46 ..
  -rw-r--r--  1 user  staff  4832 Oct 20 13:47 google.pem

$ /usr/local/opt/openssl@1.1/bin/c_rehash ./

  Doing ./

$ ls -al

  total 16
  drwxr-xr-x  4 user  staff   128 Oct 20 13:58 .
  drwxr-xr-x  4 user  staff   128 Oct 20 13:46 ..
  lrwxr-xr-x  1 user  staff    10 Oct 20 13:58 f6dbf7a7.0 -> google.pem
  -rw-r--r--  1 user  staff  4832 Oct 20 13:47 google.pem

注意,c_rehash命令创建了以下符号链接:f6dbf7a7.0 -> google.pem。

您还可以用下面的命令代替c_rehash实用程序,不过请注意,下面的命令只处理*。Pem文件,而c_rehash实用程序将处理.pem, .crt, .cer或.crl文件:

for file in *.pem; do ln -s $file `openssl x509 -hash -noout -in $file`.0; done

如果您现在配置http。将sslCAPath导入到包含上述符号链接的目录,git将获取证书文件:

# contents of /etc/gitconfig
[http]
        sslCAPath = /path/to/ssl/cert/directory/

也可以配置http。sslCAPath使用一个环境变量:

export GIT_SSL_CAPATH=/path/to/ssl/cert/directory/

以下答案摘自迈克尔·考夫曼(Michael Kauffman)撰写的文章。

使用带有公司SSL证书的Git for Windows

问题:

如果你有一个公司SSL证书,想要从控制台或VSCode克隆你的repo,你会得到以下错误:

致命:无法访问“https://myserver/tfs/DefaultCollection/_git/Proj/”:SSL证书问题:无法获得本地颁发者证书

解决方案:

将根自签名证书导出到文件中。您可以在浏览器中完成此操作。 定位“ca-bundle”。crt”文件(当前版本C:\Program Files\Git\usr\ssl\certs,但在过去已更改)。将文件复制到您的用户配置文件。用文本编辑器(如VSCode)打开它,并将导出的证书的内容添加到文件的末尾。

现在我们必须配置git来使用新文件:

Git配置——global http。sslCAInfo C: /用户/ < yourname > / ca-bundle.crt

这将把以下条目添加到用户配置文件根目录下的.gitconfig文件中。

(http) sslCAInfo = C:/Users/<yourname>/ bat .crt

您可以像这样添加自签名证书,然后再按…

git config --global http.sslCAInfo "C:\Program Files\Git\usr\ssl\cert.pem"