本文记录一些使用Git Bash for Windows的小毛病(以及一些技巧)。按照相关软件进行分类
硬盘直接放在根目录下了,所以D:\dev在git bash中是/d/dev
cd /d/dev
.exe后缀,影响自动补全参考这个问答 https://superuser.com/a/1119557/1644951
解决办法是在.bashrc中增加一个开关shopt -s completion_strip_exe,就像这样:
echo ‘shopt -s completion_strip_exe’ >> ~/.bashrc
然后重启git bash就可以了。
参考 https://learn.microsoft.com/en-us/windows/terminal/dynamic-profiles#git-bash
git for windows在安装时会提供”add profile to windows terminal“,所以重装git也可以解决问题。我选择直接编辑windows terminal的JSON配置项。
docker run --rm -it ubuntu:jammy /bin/bash 不能运行原因:/bin/bash 会被git bash翻译成类似 C:/Program Files/Git/usr/bin/bash 的路径。
解决办法:使用双斜杠开头,避免git bash的翻译。
docker run --rm -it ubuntu:jammy //bin/bash
# 注意这里 -----------------------^
还有一些别的解决办法,请参考这篇文章:https://www.pascallandau.com/blog/setting-up-git-bash-mingw-msys2-on-windows/#the-path-conversion-issue
kubectl apply -f xxx<tab> 补全时报错问题:我安装了kubectl,并且在.bashrc中增加了下面的completion脚本实现了代码补全
# kubectl completion
source <(kubectl completion bash)
但是我发现,当我尝试使用tab补全文件路径时,比如下面的操作,就会报错
kubectl apply -f crd<tab>
# bash: _filedir: command not found
原因:git bash没有完整的bash-completion,所以类似_filedir的函数是没有的。你可以自己实现一个。
将下面的代码放在.bashrc的最前面,重启bash即可。
# Minimal _filedir function for file completion
_filedir() {
# If -d is passed, only complete directories
if [[ "$1" == "-d" ]]; then
COMREPLY=( $(compgen -d -- "$cur") )
else
COMREPLY=( $(compgen -f -- "$cur") )
fi
}
问题:我想快速创建一个pod用于调试
kubectl run bash -it --image=192.168.1.29:5700/e2e-test-images/agnhost:2.39 --rm -- /bin/bash
但是我发现容器启动失败。使用kubectl logs bash发现报错Error: unknown command "C:/Program Files/Git/usr/bin/bash" for "app";我猜测是git bash把/bin/bash改写成windows的目录了。我换成双斜杠
kubectl run bash -it --image=192.168.1.29:5700/e2e-test-images/agnhost:2.39 --rm -- //bin/bash
发现也不行:Error: unknown command "//bin/bash" for "app"。
原因:kubectl没有考虑到git bash的这种尴尬场景。作为替代方案,将上面的命令换成run + exec能解决问题:
kubectl run bash --image=192.168.1.29:5700/e2e-test-images/agnhost:2.39
# exec这里使用双斜杠是可以的
kubectl exec -it bash //bin/bash
问题:我从$KUBECONFIG指向的文件中抽取出了client.pem,key.pem,ca.pem,但是当我使用curl调用k8s的接口时,报错0x80092002:
admin@REDBOX MINGW64 /d/learning/lfs258/01-curl
$ curl.exe --cert ./client.pem --key ./key.pem --cacert ./ca.pem https://192.168.58.11:6443/api/v1/pods -v
* Trying 192.168.58.11:6443...
* Connected to 192.168.58.11 (192.168.58.11) port 6443
* schannel: disabled automatic use of client certificate
* schannel: Failed to import cert file ./client.pem, last error is 0x80092002
* Closing connection
curl: (58) schannel: Failed to import cert file ./client.pem, last error is 0x80092002
原因:git-bash中的curl使用了windows系统的schannel作为ssl库,但是这个库不支持pem格式的证书。
解决方法:参考 https://stackoverflow.com/q/71325829/11395129
我的解决办法是,去 https://curl.se/windows/ 下载完整版的cURL程序zip包,解压到某个目录,然后在~/.bashrc中修改PATH变量,使得git-bash优先使用我下载的cURL。
# 我的cURL在这里
/d/tools/curl-8.15.0_4-win64-mingw/bin/curl --version
# curl 8.15.0 (x86_64-w64-mingw32) libcurl/8.15.0 LibreSSL/4.1.0 zlib/1.3.1.zlib-ng brotli/1.1.0 zstd/1.5.7 WinIDN libpsl/0.21.5 libssh2/1.11.1 nghttp2/1.66.0 ngtcp2/1.14.0 nghttp3/1.11.0
# Release-Date: 2025-07-16
# Protocols: dict file ftp ftps gopher gophers http https imap imaps ipfs ipns ldap ldaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp ws wss
# Features: alt-svc AsynchDNS brotli CAcert HSTS HTTP2 HTTP3 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM PSL SPNEGO SSL SSLS-EXPORT SSPI threadsafe UnixSockets zstd
cat << EOF >> ~/.bashrc
# use my curl
export PATH="/d/tools/curl-8.15.0_4-win64-mingw/bin:$PATH"
EOF
然后重启git-bash,再执行命令就可以了