Featured image of post Git 安装与卸载完整指南 🚀

Git 安装与卸载完整指南 🚀

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

# 安装 Git 核心包
sudo apt install git

# 安装 Git 完整套件(推荐)
sudo apt install git-all

# 验证安装版本
git --version

🗑️ 卸载 Git:

1
2
3
4
5
6
7
8
9
# 完全卸载 Git 及相关依赖
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
# CentOS/RHEL(旧版本)
sudo yum install git

# CentOS 8+/RHEL 8+ 使用 dnf
sudo dnf install git

# Fedora 系统
sudo dnf install git

# 安装开发工具组(包含完整 Git)
sudo dnf groupinstall "Development Tools"

🗑️ 卸载 Git:

1
2
3
4
5
# CentOS/RHEL
sudo yum remove git

# 使用 dnf 的系统
sudo dnf remove git

🎨 Arch Linux/Manjaro

📥 安装 Git:

1
2
3
4
5
# 使用 pacman 安装
sudo pacman -S git

# 安装 Git 扩展工具
sudo pacman -S git-extras

🗑️ 卸载 Git:

1
2
3
4
5
# 卸载 Git
sudo pacman -R git

# 彻底卸载及相关配置
sudo pacman -Rns git

二、🪟 Windows 手动安装 Git

📥 下载 Git

  1. 🌐 访问 Git 官方下载页面
  2. 💾 下载 Windows 版本安装程序(推荐 64-bit 版本)
  3. 🚀 运行下载的 .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

  1. 控制面板卸载

    • 打开 “控制面板” → “程序和功能”
    • 找到 “Git” 程序 → 点击 “卸载”
  2. 命令行卸载

    1
    2
    3
    4
    5
    
    # 查找 Git 安装信息
    wmic product where "name like '%Git%'" get name,version
    
    # 卸载 Git(管理员权限)
    wmic product where "name='Git'" call uninstall /nointeractive
    
  3. 清理残留文件

    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
# 1. 设置 PowerShell 执行策略
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

# 2. 安装 Scoop
irm get.scoop.sh | iex

# 3. 如果网络问题,使用国内镜像
irm https://gitee.com/glsnames/scoop-installer/raw/master/bin/install.ps1 | iex

# 4. 验证安装
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

# 安装基础 Git
scoop install git

# 安装完整版 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 和所有软件
scoop update *
scoop update git

# 卸载 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
# 1. 检查 Git 版本
git --version

# 2. 验证 Git 可执行文件位置
which git        # Linux/Mac
where git        # Windows

# 3. 检查 Git 帮助系统
git --help

# 4. 验证核心功能
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"        # VS Code
git config --global core.editor "vim"                # Vim
git config --global core.editor "notepad++.exe -multiInst -nosession"  # Notepad++

# 配置行结束符(跨平台协作)
git config --global core.autocrlf true               # Windows
git config --global core.autocrlf input              # Linux/Mac

# 启用颜色输出
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
# Windows 检查 PATH
echo $env:PATH

# Linux/Mac 检查 PATH
echo $PATH

# 重新加载配置
source ~/.bashrc        # Bash
source ~/.zshrc         # Zsh

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

# 永久解决方案 - 安装证书
# Windows: 安装 Git 时选择 "Use the OpenSSL library"
# Linux: sudo apt-get install ca-certificates

# 配置 Git 使用系统证书
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
# Windows 开发者配置
git config --global core.autocrlf true

# Linux/Mac 开发者配置  
git config --global core.autocrlf input

# 禁用换行符转换
git config --global core.autocrlf false

# 刷新 Git 索引
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 密钥
ssh-keygen -t ed25519 -C "your-email@example.com"

# 或者使用 RSA
ssh-keygen -t rsa -b 4096 -C "your-email@example.com"

# 启动 SSH 代理
eval "$(ssh-agent -s)"

# 添加私钥到代理
ssh-add ~/.ssh/id_ed25519

# 复制公钥到剪贴板
cat ~/.ssh/id_ed25519.pub | clip    # Windows
cat ~/.ssh/id_ed25519.pub | pbcopy  # Mac

5. 💾 缓存和索引问题

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# 清理 Git 缓存
git rm -r --cached .

# 重置 Git 索引
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
# 使用 Git 版本管理器(Linux/Mac)
# 安装 git-up
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
# 禁用不安全的 HTTP 协议
git config --global http.sslVerify true
git config --global http.sslBackend schannel

# 设置凭证缓存超时
git config --global credential.helper 'cache --timeout=3600'

# 启用 GPG 签名提交
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
# 备份 Git 配置
cp ~/.gitconfig ~/.gitconfig.backup
cp -r ~/.ssh ~/.ssh.backup

# 导出已安装的 Git 扩展列表
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 以获取最新功能和安全修复。🚀

下一步建议

  1. 📚 学习 Git 基础命令
  2. 🔗 配置 SSH 密钥连接到远程仓库
  3. 👥 了解团队协作工作流
  4. 🛠️ 探索 Git 图形化工具
最后更新于 2025-10-06