目的:由于电脑设置了代理,有时候是7890,有时候是10809,有时候关着的,所以弄个脚本执行一下自动配置
使用的编码为GB18030
保存,输出不会乱码
@echo off
setlocal EnableDelayedExpansion
rem 检查Windows是否设置了代理
reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" | findstr /C:"ProxyServer" >nul 2>&1
if %errorlevel% equ 0 (
rem 代理存在
rem 判断是否开启代理
for /f "tokens=3" %%a in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable ^| findstr /C:"REG_DWORD"') do set proxy_enable=%%a
rem 如果开启就配置,否则就关闭
if "!proxy_enable!" == "0x1" (
rem 获取代理地址和端口
for /f "tokens=3" %%b in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer ^| findstr /C:"REG_SZ"') do set proxy_value=%%b
set proxy=!proxy_value: =!
rem 配置Git代理
git config --global http.proxy http://!proxy!
if %errorlevel% neq 0 (
echo Git HTTP代理配置失败。
)
git config --global https.proxy https://!proxy!
if %errorlevel% neq 0 (
echo Git HTTPS代理配置失败。
)
) else (
rem 取消Git代理配置
echo 取消Git代理配置
git config --global --unset http.proxy
if %errorlevel% neq 0 (
echo Git HTTP代理取消失败。
)
git config --global --unset https.proxy
if %errorlevel% neq 0 (
echo Git HTTPS代理取消失败。
)
)
) else (
echo 检测失败
)