Git 下载安装与配置教程
1. Git 简介
Git 是一个分布式版本控制系统,由 Linus Torvalds 于 2005 年创建,用于高效管理从小型到大型项目的代码版本。它支持:
- ✅ 分布式开发,每个开发者拥有完整仓库
- ✅ 强大的分支管理能力
- ✅ 快速高效的数据存储
- ✅ 数据完整性保证
2. 下载 Git
官方下载地址
访问 Git 官网:https://git-scm.com/downloads
各平台下载链接速查
| 平台 | 下载链接 | 文件类型 |
|---|---|---|
| Windows | https://git-scm.com/download/win | .exe 安装包 |
| Windows 便携版 | https://git-scm.com/download/win | .zip / .7z |
| macOS | https://git-scm.com/download/mac | .dmg 安装包 |
| Linux | https://git-scm.com/download/linux | 包管理器安装 |
国内镜像加速
- 淘宝镜像:https://registry.npmmirror.com/binary.html?path=git-for-windows/
- 清华大学镜像:https://mirrors.tuna.tsinghua.edu.cn/github-release/git-for-windows/git/
3. Windows 系统安装
方式一:官方安装包(推荐)
步骤 1:下载安装包
访问 https://git-scm.com/download/win,选择对应系统位数:
- 64 位:
Git-2.x.x-64-bit.exe - 32 位:
Git-2.x.x-32-bit.exe
步骤 2:运行安装程序
双击 .exe 文件,点击 Next
步骤 3:选择安装路径
默认路径:C:\Program Files\Git\
可自定义路径,建议保持默认
步骤 4:选择组件(重要!)
建议勾选以下组件:
- ✅ Git Bash Here(右键菜单)
- ✅ Git GUI Here(右键菜单)
- ✅ Git LFS(大文件支持)
- ✅ Associate .git_ configuration files_*(关联配置文件)
- ✅ Associate .sh files to be run with Bash(关联 Shell 脚本)
💡 特别注意:确保勾选
Windows Explorer integration下的两个选项,方便在文件夹中右键操作。
步骤 5:选择默认编辑器
选择 Git 默认使用的文本编辑器:
- Vim(默认,但初学者较难上手)
- Notepad++(推荐)
- Visual Studio Code(强烈推荐,如果已安装)
- Sublime Text 等
建议选择 Visual Studio Code 或 Notepad++。
步骤 6:调整 PATH 环境变量(关键!)
选择 Git 在 PATH 中的使用方式:
| 选项 | 说明 | 推荐 |
|---|---|---|
| Git Bash Only | 仅在 Git Bash 中使用 | ❌ |
| Git from command line and also from 3rd-party software | 从命令行和第三方软件使用 Git(推荐) | ✅ 推荐 |
| Use Git and optional Unix tools from the Command Prompt | 覆盖 Windows 原生工具(可能冲突) | ⚠️ 不推荐新手 |
选择第二项:Git from the command line and also from 3rd-party software
步骤 7:选择 HTTPS 传输后端
- Use the OpenSSL library(默认,推荐)
- Use the native Windows Secure Channel library
选择默认的 OpenSSL 即可。
步骤 8:配置行结束符转换
| 选项 | 说明 | 推荐 |
|---|---|---|
| Checkout Windows-style, commit Unix-style | 检出时转 CRLF,提交时转 LF | ✅ 推荐 |
| Checkout as-is, commit Unix-style | 检出不变,提交转 LF | |
| Checkout as-is, commit as-is | 不做任何转换 | ❌ 不推荐 |
选择第一项:Checkout Windows-style, commit Unix-style
步骤 9:选择终端模拟器
- Use MinTTY(默认,推荐)— 功能更丰富的终端
- Use Windows’ default console window — Windows 原生终端
选择 Use MinTTY。
步骤 10:配置额外选项
- ✅ Enable file system caching(启用文件缓存)
- ✅ Enable symbolic links(启用符号链接,需管理员权限)
步骤 11:实验性功能
建议保持默认(不勾选),直接点击 Install 开始安装。
步骤 12:完成安装
点击 Finish 完成安装。
方式二:便携版(ZIP 免安装)
适用于 多版本共存、U 盘便携使用 或 无管理员权限 的场景。
步骤 1:下载便携版
访问 https://git-scm.com/download/win 下载:
PortableGit-2.x.x-64-bit.7z.exe(自解压格式)- 或
PortableGit-2.x.x-64-bit.7z(需 7-Zip 解压)
步骤 2:解压到目标目录
解压到目标目录,例如:
D:\Git\
解压后目录结构:
D:\Git\
├── git-bash.exe # Git Bash 启动文件
├── git-cmd.exe # Git CMD 启动文件
├── bin\ # Git 核心命令
├── cmd\ # Git 辅助命令
├── mingw64\ # MinGW 工具链
└── usr\ # Unix 工具
步骤 3:配置环境变量(可选但推荐)
- 右键「此电脑」→「属性」→「高级系统设置」→「环境变量」
- 在「系统变量」或「用户变量」的
Path中添加:
D:\Git\bin
D:\Git\cmd
- 点击「确定」保存
步骤 4:验证安装
打开 CMD 或 PowerShell:
git --version
# 输出示例:git version 2.45.0.windows.1
便携版特点
| 优点 | 缺点 |
|---|---|
| ✅ 免安装,解压即用 | ❌ 需手动配置环境变量 |
| ✅ 方便多版本共存 | ❌ 不会自动创建右键菜单 |
| ✅ 可放在 U 盘便携使用 | ❌ 不会自动关联文件类型 |
| ✅ 无需管理员权限 | ❌ 部分功能依赖系统组件 |
方式三:包管理器
使用 Winget(Windows 11/10 内置):
winget install --id Git.Git -e --source winget
使用 Chocolatey:
choco install git
使用 Scoop:
scoop install git
4. macOS 系统安装
方式一:官方安装包
- 访问 https://git-scm.com/download/mac
- 下载
.dmg文件 - 双击
.dmg并运行安装程序 - 按照提示完成安装
方式二:使用 Homebrew(推荐)
# 安装 Git
brew install git
# 验证安装
git --version
方式三:使用 Xcode Command Line Tools
xcode-select --install
系统会弹出安装提示,点击安装即可(包含 Git)。
方式四:使用 MacPorts
sudo port install git
5. Linux 系统安装
Ubuntu / Debian
# 使用 apt 安装
sudo apt update
sudo apt install git
# 验证安装
git --version
CentOS / RHEL / Fedora
# CentOS / RHEL
sudo yum install git
# Fedora
sudo dnf install git
# 验证安装
git --version
从源码编译安装(获取最新版本)
# 安装依赖
sudo apt install make libssl-dev libghc-zlib-dev libcurl4-gnutls-dev libexpat1-dev gettext unzip
# 下载源码
wget https://github.com/git/git/archive/v2.45.0.tar.gz
tar -xzf v2.45.0.tar.gz
cd git-2.45.0
# 编译安装
make prefix=/usr/local all
sudo make prefix=/usr/local install
# 验证
git --version
6. 首次使用配置
Git 安装完成后,需要进行首次配置,设置用户身份信息(必须)。
6.1 配置用户名和邮箱(必须)
# 配置全局用户名
git config --global user.name "Your Name"
# 配置全局邮箱
git config --global user.email "your_email@example.com"
⚠️ 注意:邮箱需与远程仓库(GitHub/GitLab)注册邮箱一致,否则提交记录无法关联。
6.2 配置默认编辑器
# 使用 VS Code
git config --global core.editor "code --wait"
# 使用 Notepad++(Windows)
git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"
# 使用 Vim
git config --global core.editor "vim"
6.3 配置行结束符
# Windows 推荐配置
git config --global core.autocrlf true
# macOS / Linux 推荐配置
git config --global core.autocrlf input
6.4 配置默认分支名称
# 将默认分支名从 master 改为 main
git config --global init.defaultBranch main
6.5 配置颜色输出
# 开启彩色输出
git config --global color.ui auto
6.6 配置差异分析工具
# 使用 VS Code 作为 diff 工具
git config --global diff.tool vscode
git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'
# 使用默认工具(Windows)
git config --global merge.tool kdiff3
6.7 查看所有配置
# 查看所有配置
git config --list
# 查看全局配置
git config --global --list
# 查看特定配置项
git config user.name
6.8 配置文件位置
| 级别 | 文件位置 | 说明 |
|---|---|---|
| 系统级 | /etc/gitconfig |
所有用户共用(需管理员权限) |
| 用户级(全局) | ~/.gitconfig 或 ~/.config/git/config |
当前用户所有仓库共用 |
| 项目级(本地) | 项目目录/.git/config |
仅当前仓库使用(优先级最高) |
7. 配置 SSH 密钥
通过 SSH 方式连接远程仓库(如 GitHub、GitLab)更加安全便捷。
7.1 生成 SSH 密钥
# 生成 RSA 密钥(推荐)
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# 或生成 Ed25519 密钥(更安全、更快)
ssh-keygen -t ed25519 -C "your_email@example.com"
7.2 设置密钥文件名和密码
Enter file in which to save the key (~/.ssh/id_rsa): [按 Enter 使用默认]
Enter passphrase (empty for no passphrase): [输入密码,或直接回车跳过]
7.3 查看并复制公钥
# 查看公钥内容
cat ~/.ssh/id_rsa.pub
# 或复制到剪贴板
# Windows(PowerShell)
cat ~/.ssh/id_rsa.pub | clip
# macOS
pbcopy < ~/.ssh/id_rsa.pub
# Linux(需安装 xclip)
xclip -selection clipboard < ~/.ssh/id_rsa.pub
7.4 添加到远程仓库平台
GitHub
- 登录 GitHub → 右上角头像 → Settings
- 左侧菜单 → SSH and GPG keys
- 点击 New SSH Key
- 粘贴公钥内容,保存
GitLab
- 登录 GitLab → 右上角头像 → Preferences
- 左侧菜单 → SSH Keys
- 粘贴公钥内容,保存
Gitee(码云)
- 登录 Gitee → 右上角头像 → 设置
- 左侧菜单 → SSH 公钥
- 粘贴公钥内容,保存
7.5 测试 SSH 连接
# 测试 GitHub
ssh -T git@github.com
# 输出:Hi username! You've successfully authenticated...
# 测试 GitLab
ssh -T git@gitlab.com
# 测试 Gitee
ssh -T git@gitee.com
7.6 配置多 SSH Key(针对多个平台)
编辑 ~/.ssh/config 文件:
# GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_github
# GitLab
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa_gitlab
# Gitee
Host gitee.com
HostName gitee.com
User git
IdentityFile ~/.ssh/id_rsa_gitee
8. Git 别名配置
设置常用命令的简短别名,提升工作效率:
# 基础别名
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.df diff
# 增强别名
git config --global alias.lg "log --oneline --graph --all --decorate"
git config --global alias.unstage "reset HEAD --"
git config --global alias.last "log -1 HEAD"
git config --global alias.hist "log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short"
# 查看所有别名
git config --global --get-regexp alias
9. 验证安装与测试
9.1 检查版本
git --version
# 输出示例:git version 2.45.0.windows.1
9.2 查看配置信息
git config --list
9.3 创建测试仓库
# 创建测试目录
mkdir git-test
cd git-test
# 初始化仓库
git init
# 创建测试文件
echo "Hello Git" > README.md
# 添加到暂存区
git add README.md
# 提交
git commit -m "first commit"
# 查看状态
git status
# 查看提交日志
git log
10. 常见问题
Q1:git 命令无法识别
- Windows:检查环境变量 PATH 是否包含 Git 安装路径
- 重启终端:安装后需重新打开命令行窗口
- 便携版:确保已将 Git 的
bin和cmd目录添加到 PATH
Q2:中文乱码问题
# 设置编码
git config --global core.quotepath false
git config --global gui.encoding utf-8
git config --global i18n.commit.encoding utf-8
git config --global i18n.logoutputencoding utf-8
# Windows 终端建议设置
set LESSCHARSET=utf-8
Q3:SSL 证书错误
# 临时关闭 SSL 验证(不推荐)
git config --global http.sslVerify false
# 或更新系统根证书
Q4:提交时提示 “Please tell me who you are”
未配置用户名和邮箱,执行:
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"
Q5:如何卸载 Git?
Windows(安装版):
- 控制面板 → 程序和功能 → 卸载 Git
Windows(便携版):
- 直接删除解压文件夹,移除环境变量中对应的 PATH
macOS(Homebrew):
brew uninstall git
Linux(apt):
sudo apt remove git
Q6:如何升级 Git?
Windows: 下载新版安装包,覆盖安装即可
macOS(Homebrew):
brew upgrade git
Linux(apt):
sudo apt update
sudo apt upgrade git
Q7:Git GUI 工具推荐
| 工具 | 平台 | 说明 |
|---|---|---|
| Git GUI | 全平台 | Git 自带,轻量基础 |
| Sourcetree | Win/Mac | 免费,功能全面 |
| GitKraken | 全平台 | 界面美观,功能强大(收费) |
| GitHub Desktop | Win/Mac | GitHub 官方出品 |
| TortoiseGit | Win | 集成到文件管理器右键菜单 |
| Fork | Win/Mac | 轻量快速,体验优秀 |

