本文覆盖Windows/macOS/Linux三大平台高频问题,附终端命令+故障排查思维导图
一、安装前的关键检查
-
系统与硬件验证
- Windows:
winver
查看系统版本(需Win8+) - macOS:
sw_vers -productVersion
(建议10.15+) - Linux:
cat /etc/os-release
(内核≥3.0)
避坑重点:内存≥2GB,预留500MB磁盘空间
- Windows:
-
安装包安全下载
务必从官网获取:
ini# 官方镜像(国内加速) https://registry.npmmirror.com/binary.html?path=git-for-windows/
二、Windows专项避坑
-
安装路径陷阱
- ❌ 错误示例:
C:\用户\张三\git
(含中文路径) - ✅ 正确示例:
C:\DevTools\Git
- ❌ 错误示例:
-
环境变量配置
安装时勾选:
example.com/git-env-opt...
必须选择"Use Git from Windows Command Prompt" -
换行符自动转换
核心设置:
csharp# 全局禁用自动转换(推荐开发者) git config --global core.autocrlf false
安装时选择"Checkout as-is"避免乱码
三、macOS避坑指南
-
彻底卸载Xcode旧版Git
bash# 删除预装版本 sudo rm -rf /usr/bin/git* # Homebrew安装最新版 brew install git
-
SSH密钥权限修复
bashchmod 700 ~/.ssh chmod 600 ~/.ssh/id_rsa # 关键步骤!
四、Linux避坑要点
-
依赖缺失导致编译失败
Ubuntu/Debian需提前安装:
sudo apt install libcurl4-openssl-dev zlib1g-dev
-
避免Root操作风险
bash# 创建专用用户 sudo useradd -m gituser sudo -u gituser git clone <repo>
五、必做的四大基础配置
-
身份信息设置(首次必须!)
arduinogit config --global user.name "YourName" git config --global user.email "you@example.com"
-
代理配置(科学上网场景)
php# HTTP代理 git config --global http.proxy http://127.0.0.1:7890 # 重置命令 git config --global --unset http.proxy
六、安装验证四步法
bash
# 1. 验证版本
git --version
# 2. 检查核心配置
git config --list | grep -E "user.name|user.email"
# 3. 测试SSH连通性
ssh -T git@github.com
# 4. 基础功能测试
mkdir test-repo && cd test-repo
git init
echo "# Test" > README.md
git add . && git commit -m "init"
七、高效工作流优化
-
常用别名配置
csharpgit config --global alias.st status git config --global alias.br branch git config --global alias.ci commit
-
凭证缓存(避免重复输密码)
csharp# 缓存15分钟 git config --global credential.helper cache # 缓存1小时 git config --global credential.helper 'cache --timeout=3600'
-
大文件管理(>100MB需用LFS)
arduinogit lfs install git lfs track "*.psd"
八、高频故障排查表
错误现象 | 解决方案 |
---|---|
fatal: unable to access... SSL_ERROR |
git config --global http.sslVerify false |
Permission denied (publickey) |
执行 chmod 600 ~/.ssh/id_rsa |
warning: LF will be replaced by CRLF |
设置 core.autocrlf=false |
git: command not found |
检查环境变量PATH是否包含git路径 |
九、终极排错工具
-
日志分析
- Windows:
C:\ProgramData\Git\logs
- Linux/macOS:
/var/log/git/
或安装临时目录
- Windows:
-
网络诊断
csharp# 测试GitHub连通性 curl -v https://github.com # 国内镜像替代方案 git config --global url."https://hub.fastgit.xyz/".insteadOf "https://github.com/"
资料推荐