Git安装与配置完全指南
原创...大约 5 分钟
摘要
本文详细介绍了在各主流操作系统(Linux、Windows、macOS)上安装、配置和卸载Git的完整方法。包含包管理器安装、源码编译安装以及常见问题的解决方案,帮助开发者快速搭建高效的版本控制环境。
1. Git简介
Git是由Linux之父Linus Torvalds开发的分布式版本控制系统,具有以下特点:
- 分布式架构:每个开发者都拥有完整的代码仓库副本
- 高效性能:即使对大型项目也能快速处理
- 数据完整性:通过SHA-1哈希算法确保数据完整性
- 强大的分支管理:轻量级分支创建与合并
- 灵活的工作流:支持多种开发模式
2. 系统要求
操作系统 | 最低要求 | 推荐配置 |
---|---|---|
Linux | 内核2.6.23+,glibc 2.12+ | 现代Linux发行版 |
Windows | Windows 7+ | Windows 10/11 |
macOS | OS X 10.9+ | macOS 10.14+ |
3. Linux系统安装Git
3.1 CentOS/RHEL/Fedora
使用包管理器安装(推荐)
# CentOS/RHEL
sudo yum update -y
sudo yum install git -y
# Fedora
sudo dnf install git -y
# 验证安装
git --version
# 卸载Git
sudo yum remove git -y
sudo yum autoremove -y
使用IUS仓库安装最新版本
# 安装IUS仓库
sudo yum install -y https://repo.ius.io/ius-release-el$(rpm -E '%{rhel}').rpm
# 安装最新版Git
sudo yum install -y git2u
# 验证安装
git --version
3.2 Ubuntu/Debian
使用APT安装
# 更新包列表
sudo apt update
# 安装Git
sudo apt install git -y
# 验证安装
git --version
# 卸载Git
sudo apt purge git -y
sudo apt autoremove -y
使用PPA安装最新版本
# 添加Git PPA
sudo add-apt-repository ppa:git-core/ppa -y
sudo apt update
# 安装Git
sudo apt install git -y
# 验证安装
git --version
3.3 从源码编译安装
适用于需要最新版本或特定功能的情况:
# 安装编译依赖
# CentOS/RHEL
sudo yum groupinstall "Development Tools" -y
sudo yum install gettext-devel openssl-devel perl-CPAN perl-devel zlib-devel curl-devel expat-devel -y
# Ubuntu/Debian
sudo apt install build-essential libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext unzip -y
# 下载最新的Git源码
cd /tmp
wget https://github.com/git/git/archive/refs/tags/v2.43.0.tar.gz
tar -xzf v2.43.0.tar.gz
cd git-2.43.0
# 编译安装
make configure
./configure --prefix=/usr/local
make -j$(nproc)
sudo make install
# 添加到PATH
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
# 验证安装
git --version
# 卸载源码安装的Git
sudo rm -rf /usr/local/bin/git*
sudo rm -rf /usr/local/share/git*
4. Windows安装Git
4.1 使用安装程序
- 访问Git官网下载页
- 下载最新版本的安装程序
- 运行安装向导,注意以下选项:
- 选择安装位置
- 选择组件(建议保留默认)
- 选择默认编辑器(推荐VS Code或Notepad++)
- 调整PATH环境(推荐"Git from the command line and also from 3rd-party software")
- 选择HTTPS传输后端(推荐OpenSSL)
- 配置行尾处理(Windows推荐"Checkout Windows-style, commit Unix-style")
4.2 使用Chocolatey包管理器
# 以管理员身份运行PowerShell
Set-ExecutionPolicy Bypass -Scope Process -Force
choco install git -y
# 验证安装
git --version
# 卸载Git
choco uninstall git -y
4.3 使用Windows Subsystem for Linux (WSL)
# 启用WSL
wsl --install
# 在WSL中安装Git
wsl sudo apt update
wsl sudo apt install git -y
5. macOS安装Git
5.1 使用Homebrew
# 安装Homebrew(如果未安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 安装Git
brew install git
# 验证安装
git --version
# 卸载Git
brew uninstall git
5.2 使用MacPorts
# 安装MacPorts(如果未安装)
# 先从 https://www.macports.org/install.php 下载安装程序
# 安装Git
sudo port install git
# 验证安装
git --version
# 卸载Git
sudo port uninstall git
5.3 使用Xcode Command Line Tools
# 安装Command Line Tools
xcode-select --install
# 验证安装
git --version
6. Git基础配置
6.1 必要配置
# 设置用户名和邮箱(全局)
git config --global user.name "你的用户名"
git config --global user.email "your.email@example.com"
# 设置默认编辑器
git config --global core.editor "vim" # 或 "nano", "code --wait"等
# 设置默认分支名
git config --global init.defaultBranch main
6.2 提升使用体验的配置
# 设置颜色
git config --global color.ui true
# 设置别名
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
# 配置凭证存储
git config --global credential.helper store # 永久存储
# 或
git config --global credential.helper cache --timeout=3600 # 缓存1小时
# 设置拉取策略
git config --global pull.rebase false # 合并(默认)
# 或
git config --global pull.rebase true # 变基
6.3 解决常见问题的配置
# 解决中文文件名乱码
git config --global core.quotepath false
# 解决行尾符问题
git config --global core.autocrlf input # Linux/macOS
# 或
git config --global core.autocrlf true # Windows
# 配置代理(如需要)
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy
7. 验证安装
# 查看Git版本
git --version
# 查看所有配置
git config --list
# 创建测试仓库
mkdir git-test && cd git-test
git init
echo "# Git测试仓库" > README.md
git add README.md
git commit -m "初始提交"
git branch -M main
8. 常见问题排查
8.1 权限问题
# Linux/macOS权限问题
sudo chown -R $(whoami) ~/.gitconfig
sudo chmod -R 755 /usr/local/bin/git*
# Windows权限问题(管理员PowerShell)
icacls "C:\Program Files\Git" /grant Everyone:F /T
8.2 SSL证书问题
# 忽略SSL证书验证(不推荐用于生产环境)
git config --global http.sslVerify false
# 更新CA证书
sudo update-ca-certificates # Linux
8.3 性能问题
# 提高大型仓库性能
git config --global core.preloadindex true
git config --global core.fscache true
git config --global gc.auto 256
8.4 完全卸载Git
Linux
# CentOS/RHEL
sudo yum remove git* -y
sudo yum autoremove -y
# Ubuntu/Debian
sudo apt purge git* -y
sudo apt autoremove -y
# 删除配置文件
rm -rf ~/.gitconfig ~/.git-credentials
Windows
# 使用安装程序卸载
# 或使用Chocolatey
choco uninstall git -y
# 删除残留文件
Remove-Item -Path "$env:USERPROFILE\.gitconfig" -Force
Remove-Item -Path "$env:USERPROFILE\.git-credentials" -Force
macOS
# Homebrew卸载
brew uninstall git
# 删除配置文件
rm -rf ~/.gitconfig ~/.git-credentials
9. 高级配置
9.1 Git钩子
# 查看可用的钩子模板
ls -la /usr/local/share/git-core/templates/hooks/
# 创建提交前钩子示例
cat > .git/hooks/pre-commit << 'EOF'
#!/bin/sh
echo "执行代码检查..."
# 在此添加检查代码的命令
exit 0
EOF
chmod +x .git/hooks/pre-commit
9.2 配置多账户
# 创建~/.gitconfig
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
[includeIf "gitdir:~/personal/"]
path = ~/.gitconfig-personal
# 创建~/.gitconfig-work
[user]
name = Your Work Name
email = your.work.email@company.com
# 创建~/.gitconfig-personal
[user]
name = Your Personal Name
email = your.personal.email@example.com
10. 总结
Git的安装方法因操作系统而异,但基本配置相似。选择适合你的安装方法,完成必要配置后即可开始使用。定期更新Git以获取最新功能和安全修复。
Powered by Waline v3.6.0