解决 Windows 下 Git 报错及优化

后知后觉 现有 1 评论

习惯 macOS 后,部分时候使用 Windows 反倒出现各种不适应,本文记录下使用过程中遇到的问题。

报错

在为 Git 配置 GPG 密钥并在 Github 配置 GPG 密钥。

关于配置 Windows 下的 GPG ,请参考 Windows 下 Git 配置 GPG 秘钥

在 Git 中 commit 时使用 GPG 秘钥进行签名

$ git config commit.gpgsign true

查看所有密钥

$ gpg --list-secret-keys --keyid-format LONG

为项目关联秘钥

$ git config --global user.signingkey 0000000000000000000027FDEFF476BB0B000000

上述操作后在 commit 时遇到以下报错

Error
Commit failed - exit code 128 received, with output: 'error: cannot spawn gpg: No such file or directory
error: gpg failed to sign the data
fatal: failed to white commit object'

这是因为 Git 程序没有找到 GPG 的可执行文件导致的。

  • Windows
git config --global gpg.program 'C:\Program Files (x86)\GnuPG\bin\gpg.exe'
小贴士:后面的可执行文件地址为默认安装位置,若有修改,请自行替换即可。
  • macOS

一般情况下不会遇到这种问题,可能是 $PATH 丢失导致的。

git config --global gpg.program $(which gpg)

优化

无法显示中文

找到 Git 配置文件,在安装目录的 \etc 目录下

x64 版本默认安装在 C:\Program Files\Git\etc\bash.bashrc

在文件最后添加两行

export LANG="zh_CN.UTF-8"
export LC_ALL="zh_CN.UTF-8"

修改后重启终端即可

使用代理

HTTP(S)

终端执行

git config --global http.proxy 'socks5://127.0.0.1:10800'
git config --global https.proxy 'socks5://127.0.0.1:10800'
注意:此种配置只生效于 HTTP 方式拉取或者提交代码,对于 SSH 并不能使用代理。

SSH

为 SSH 方式配置代理

SSH 方式需要使用 connect.exe 进行隧道代理,首先需要明确一点,connect.exe 已经在 Git 安装包中预置了,无需单独下载安装。

Error
Commit failed - exit code 128 received, with output: 'error: cannot spawn gpg: No such file or directory
error: gpg failed to sign the data
fatal: failed to white commit object'

这是因为 Git 程序没有找到 GPG 的可执行文件导致的。

  • Windows
git config --global gpg.program 'C:\Program Files (x86)\GnuPG\bin\gpg.exe'
小贴士:后面的可执行文件地址为默认安装位置,若有修改,请自行替换即可。
  • macOS

一般情况下不会遇到这种问题,可能是 $PATH 丢失导致的。

git config --global gpg.program $(which gpg)

优化

无法显示中文

找到 Git 配置文件,在安装目录的 \etc 目录下

x64 版本默认安装在 C:\Program Files\Git\etc\bash.bashrc

在文件最后添加两行

export LANG="zh_CN.UTF-8"
export LC_ALL="zh_CN.UTF-8"

修改后重启终端即可

使用代理

HTTP(S)

终端执行

git config --global http.proxy 'socks5://127.0.0.1:10800'
git config --global https.proxy 'socks5://127.0.0.1:10800'
注意:此种配置只生效于 HTTP 方式拉取或者提交代码,对于 SSH 并不能使用代理。

SSH

为 SSH 方式配置代理

SSH 方式需要使用 connect.exe 进行隧道代理,首先需要明确一点,connect.exe 已经在 Git 安装包中预置了,无需单独下载安装。

cannot spawn gpg

Error
Commit failed - exit code 128 received, with output: 'error: cannot spawn gpg: No such file or directory
error: gpg failed to sign the data
fatal: failed to white commit object'

这是因为 Git 程序没有找到 GPG 的可执行文件导致的。

  • Windows
git config --global gpg.program 'C:\Program Files (x86)\GnuPG\bin\gpg.exe'
小贴士:后面的可执行文件地址为默认安装位置,若有修改,请自行替换即可。
  • macOS

一般情况下不会遇到这种问题,可能是 $PATH 丢失导致的。

git config --global gpg.program $(which gpg)

优化

无法显示中文

找到 Git 配置文件,在安装目录的 \etc 目录下

x64 版本默认安装在 C:\Program Files\Git\etc\bash.bashrc

在文件最后添加两行

export LANG="zh_CN.UTF-8"
export LC_ALL="zh_CN.UTF-8"

修改后重启终端即可

使用代理

HTTP(S)

终端执行

git config --global http.proxy 'socks5://127.0.0.1:10800'
git config --global https.proxy 'socks5://127.0.0.1:10800'
注意:此种配置只生效于 HTTP 方式拉取或者提交代码,对于 SSH 并不能使用代理。

SSH

为 SSH 方式配置代理

SSH 方式需要使用 connect.exe 进行隧道代理,首先需要明确一点,connect.exe 已经在 Git 安装包中预置了,无需单独下载安装。

connect.exe

然后修改用户配置文件

vim ~/.ssh/config

加入以下内容

ProxyCommand connect -S 127.0.0.1:10800 -a none %h %p

Host github.com
  User git
  Port 22
  Hostname github.com
  # 注意修改路径为你的路径
  IdentityFile "C:\Users\Kane\.ssh\id_rsa"
  TCPKeepAlive yes

Host ssh.github.com
  User git
  Port 443
  Hostname ssh.github.com
  # 注意修改路径为你的路径
  IdentityFile "C:\Users\Kane\.ssh\id_rsa"
  TCPKeepAlive yes
小贴士: -a none 表示不使用验证方式, -S 指定代理服务器

附录

参考链接

本文撰写于一年前,如出现图片失效或有任何问题,请在下方留言。博主看到后将及时修正,谢谢!
禁用 / 当前已拒绝评论,仅可查看「历史评论」。
  1. avatarImg 灵异故事

    日常打卡~ 加油-_-

    Firefox 79.0 Windows 10
    IP 属地 未知