Git 安装与卸载完整指南 🚀

Git 是一个开源的分布式版本控制系统,由 Linux 之父 Linus Torvalds 开发,现已成为全球开发者必备的工具之一 ✨
📋 目录导航
🚀 Git 简介与核心功能
🔥 Git 的核心优势
- 📊 版本控制:完整记录每次代码变更,支持回滚到任意历史版本
- 👥 团队协作:多人并行开发,自动合并代码变更
- 💾 分布式架构:每个开发者都拥有完整的代码仓库副本
- ⚡ 高效性能:快速处理大型项目和大量历史记录
- 🔒 数据完整性:使用 SHA-1 哈希确保代码完整性
🎯 适用场景
- 个人项目版本管理 📝
- 团队协作开发 👨💻👩💻
- 开源项目贡献 🌟
- 代码备份与恢复 💾
一、🐧 Linux 发行版安装与卸载
🎯 Ubuntu/Debian 系统
📥 安装 Git:
1 2 3 4 5 6 7 8 9 10 11
| sudo apt update
sudo apt install git
sudo apt install git-all
git --version
|
🗑️ 卸载 Git:
1 2 3 4 5 6 7 8 9
| sudo apt remove git sudo apt autoremove
sudo apt purge git
rm -rf ~/.gitconfig ~/.config/git
|
🔴 CentOS/RHEL/Fedora 系统
📥 安装 Git:
1 2 3 4 5 6 7 8 9 10 11
| sudo yum install git
sudo dnf install git
sudo dnf install git
sudo dnf groupinstall "Development Tools"
|
🗑️ 卸载 Git:
1 2 3 4 5
| sudo yum remove git
sudo dnf remove git
|
🎨 Arch Linux/Manjaro
📥 安装 Git:
1 2 3 4 5
| sudo pacman -S git
sudo pacman -S git-extras
|
🗑️ 卸载 Git:
1 2 3 4 5
| sudo pacman -R git
sudo pacman -Rns git
|
二、🪟 Windows 手动安装 Git
📥 下载 Git
- 🌐 访问 Git 官方下载页面
- 💾 下载 Windows 版本安装程序(推荐 64-bit 版本)
- 🚀 运行下载的
.exe 文件开始安装
🔧 详细安装步骤
1. 许可证协议 📄
- 仔细阅读 GNU 通用公共许可证
- 点击 “Next” 同意协议内容
2. 安装路径选择 📁
- 默认路径:
C:\Program Files\Git
- 可自定义路径,避免中文和特殊字符
3. 组件选择 🛠️
1 2 3 4 5
| ✅ Git Bash - Linux 风格命令行环境 ✅ Git GUI - 图形化界面工具 ✅ Git LFS - 大文件支持 ✅ Associate .git* - 关联 Git 配置文件 ✅ Add Git to PATH - 添加到环境变量
|
4. 开始菜单文件夹 📂
- 保持默认或自定义文件夹名称
- 建议使用 “Git” 作为文件夹名
5. 默认编辑器选择 ✏️
- Vim(默认,适合高级用户)
- Nano(新手友好)
- Visual Studio Code
- Notepad++
6. PATH 环境配置 🔧
1 2 3
| 🔹 Use Git from Git Bash only(仅限 Git Bash) 🔹 Use Git from the Windows Command Prompt(推荐) 🔹 Use Git and optional Unix tools from Windows Command Prompt
|
7. SSH 可执行文件 🔐
- Use OpenSSH(默认)
- Use external OpenSSH(如果已安装)
8. HTTPS 传输后端 🌐
- Use the OpenSSL library
- Use the native Windows Secure Channel library
9. 行结束符配置 ↵
1 2 3
| 🔹 Checkout Windows-style, commit Unix-style(推荐) 🔹 Checkout as-is, commit as-is 🔹 Checkout Windows-style, commit Windows-style
|
10. 终端模拟器 💻
- Use MinTTY(推荐,功能更完整)
- Use Windows’ default console window
11. 额外功能选项 ⚡
1 2 3
| ✅ Enable file system caching ✅ Enable Git Credential Manager ✅ Enable symbolic links
|
🗑️ 手动卸载 Git
-
控制面板卸载:
- 打开 “控制面板” → “程序和功能”
- 找到 “Git” 程序 → 点击 “卸载”
-
命令行卸载:
1 2 3 4 5
| # 查找 Git 安装信息 wmic product where "name like '%Git%'" get name,version
# 卸载 Git(管理员权限) wmic product where "name='Git'" call uninstall /nointeractive
|
-
清理残留文件:
1 2 3 4 5 6
| # 删除用户配置 rmdir /s %USERPROFILE%\.config\git rmdir /s %USERPROFILE%\.gitconfig
# 删除全局配置(如存在) rmdir /s "C:\Program Files\Git"
|
三、🔧 Windows 使用 Winget 安装 Git
🔍 检查 Winget 可用性
1 2 3 4 5
| # 检查 Winget 版本 winget --version
# 如果未找到命令,需要安装 App Installer # 从 Microsoft Store 搜索 "App Installer" 并安装
|
📥 使用 Winget 安装 Git
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| # 搜索 Git 相关包 winget search git
# 查看 Git 包详细信息 winget show Git.Git
# 安装最新版 Git winget install Git.Git
# 安装特定版本 winget install Git.Git -v 2.42.0
# 静默安装(无交互) winget install Git.Git --silent
# 指定安装位置 winget install Git.Git -l "D:\Development\Git"
|
🔄 更新与卸载
1 2 3 4 5 6 7 8 9 10 11
| # 检查可更新软件 winget upgrade
# 更新 Git winget upgrade Git.Git
# 卸载 Git winget uninstall Git.Git
# 强制卸载 winget uninstall Git.Git --force
|
🎯 Winget 优势
- ⚡ 快速安装:一键完成下载和配置
- 🔄 自动更新:轻松保持最新版本
- 📦 官方源:直接从官方仓库获取
- 🛡️ 安全可靠:微软验证的数字签名
四、📦 Windows 使用 Scoop 安装 Git
🚀 Scoop 安装指南
前提条件检查 ✅:
- Windows 7 SP1+ / Windows Server 2008+
- PowerShell 5+(推荐 PowerShell 7)
- .NET Framework 4.5+
安装步骤:
1 2 3 4 5 6 7 8 9 10 11
| Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
irm get.scoop.sh | iex
irm https://gitee.com/glsnames/scoop-installer/raw/master/bin/install.ps1 | iex
scoop --version
|
📥 使用 Scoop 安装 Git
1 2 3 4 5 6 7 8 9 10 11 12 13
| scoop bucket add main scoop bucket add extras scoop bucket add versions
scoop install git
scoop install git-with-openssh
scoop install git@2.42.0
|
🛠️ Scoop 管理命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| scoop list
scoop update * scoop update git
scoop uninstall git
scoop cache rm git scoop cleanup git
scoop info git
|
🌟 Scoop 特色功能
- 🏠 用户级安装:无需管理员权限
- 🔗 环境自动配置:自动设置 PATH 变量
- 📊 依赖管理:自动处理软件依赖
- 🔄 版本切换:轻松切换不同版本
- 🧹 干净卸载:彻底清理所有文件
五、✅ 验证安装与基础配置
🔍 安装验证步骤
1 2 3 4 5 6 7 8 9 10 11 12 13
| git --version
which git where git
git --help
git status git config --list
|
⚙️ 首次使用配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| git config --global user.name "你的用户名" git config --global user.email "your-email@example.com"
git config --global core.editor "code --wait" git config --global core.editor "vim" git config --global core.editor "notepad++.exe -multiInst -nosession"
git config --global core.autocrlf true git config --global core.autocrlf input
git config --global color.ui auto
git config --global init.defaultBranch main
|
🎨 个性化配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| 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.unstage 'reset HEAD --' git config --global alias.last 'log -1 HEAD'
git config --global push.default simple
git config --global core.preloadindex true git config --global core.fscache true
git config --global diff.tool vscode git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"
|
六、❌ 常见问题解决
1. 🚫 命令未找到错误
问题现象:
1 2
| bash: git: command not found 'git' is not recognized as an internal or external command
|
解决方案:
1 2 3 4 5 6 7 8 9
| echo $env:PATH
echo $PATH
source ~/.bashrc source ~/.zshrc
|
2. 🔐 SSL 证书问题
问题现象:
1
| SSL certificate problem: unable to get local issuer certificate
|
解决方案:
1 2 3 4 5 6 7 8 9
| git config --global http.sslVerify false
git config --global http.sslCAInfo /etc/ssl/certs/ca-certificates.crt
|
3. ⏎ 行结束符警告
问题现象:
1 2
| warning: LF will be replaced by CRLF in filename. The file will have its original line endings in your working directory.
|
解决方案:
1 2 3 4 5 6 7 8 9 10 11 12
| git config --global core.autocrlf true
git config --global core.autocrlf input
git config --global core.autocrlf false
git rm --cached -r . git reset --hard
|
4. 🔒 权限被拒绝错误
问题现象:
1 2
| Permission denied (publickey). Could not read from remote repository.
|
解决方案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| ssh-keygen -t ed25519 -C "your-email@example.com"
ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
cat ~/.ssh/id_ed25519.pub | clip cat ~/.ssh/id_ed25519.pub | pbcopy
|
5. 💾 缓存和索引问题
1 2 3 4 5 6 7 8 9 10 11 12 13
| git rm -r --cached .
git reset
git clean -fd
git fsck --full git reflog expire --expire=now --all git gc --prune=now --aggressive
|
七、💡 进阶技巧与最佳实践
🎯 安装方式选择指南
| 安装方式 |
适用场景 |
优点 |
缺点 |
| 手动安装 |
🏢 企业环境、生产服务器 |
完全控制、稳定可靠 |
配置复杂、更新麻烦 |
| Winget |
🏠 个人使用、快速部署 |
微软官方、一键安装 |
版本可能滞后 |
| Scoop |
💻 开发环境、多版本管理 |
用户级安装、干净卸载 |
需要 PowerShell |
🔧 性能优化配置
1 2 3 4 5 6 7 8 9 10 11 12
| git config --global core.compression 9 git config --global core.deltaBaseCacheLimit 2g git config --global pack.deltaCacheSize 1g
git config --global core.packedGitLimit 512m git config --global core.packedGitWindowSize 512m
git config --global http.postBuffer 524288000 git config --global https.postBuffer 524288000
|
📊 多版本 Git 管理
1 2 3 4 5 6 7
|
curl -L https://raw.github.com/aanand/git-up/master/install.sh | sh
brew install git brew install git@2.33
|
🛡️ 安全配置建议
1 2 3 4 5 6 7 8 9 10
| git config --global http.sslVerify true git config --global http.sslBackend schannel
git config --global credential.helper 'cache --timeout=3600'
git config --global commit.gpgsign true git config --global user.signingkey YOUR_GPG_KEY_ID
|
🔄 备份与迁移
1 2 3 4 5 6 7 8 9 10
| cp ~/.gitconfig ~/.gitconfig.backup cp -r ~/.ssh ~/.ssh.backup
scoop export > scoop-packages.txt winget export --output winget-packages.json
git clone --filter=blob:none https://github.com/user/repo.git
|
🎉 总结
通过本指南,您已经掌握了在各种操作系统上安装和卸载 Git 的完整流程。无论您是:
- 🐧 Linux 用户:通过包管理器轻松安装
- 🪟 Windows 用户:可选择手动、Winget 或 Scoop 多种方式
- 🔧 开发人员:使用 Scoop 管理多个开发工具
- 🏢 企业用户:手动安装确保稳定性
选择最适合您需求的安装方式,开始享受 Git 带来的高效版本控制体验!记得定期更新 Git 以获取最新功能和安全修复。🚀
下一步建议:
- 📚 学习 Git 基础命令
- 🔗 配置 SSH 密钥连接到远程仓库
- 👥 了解团队协作工作流
- 🛠️ 探索 Git 图形化工具