Git 安装指南:Windows / macOS / Linux 安装与首次配置

装 Git 只有两步:装二进制 + 首次配置。多数人卡住不是因为装不上,而是没配 user.name / user.email,第一次 commit 就报错。

最后更新:2026-10-30

Git 命令速查表 → Git 平台实战 → Git 急救手册 →

三句话装好 Git

Windows:Git for Windows

从 git-scm.com 下载安装包(国内慢可看《平台实战》的加速章节)。安装向导里建议这样选:

选项建议
默认编辑器选 VS Code / Notepad++,别用 Vim(除非你熟悉)
PATH 环境Git from the command line and also from 3rd-party software
换行符Checkout Windows-style, commit Unix-style(core.autocrlf=true)
终端默认 Git Bash 即可

装完开始菜单会多出 Git Bash / Git CMD / Git GUI,日常用 Git Bash 最省心。

git --version

macOS:两条路

xcode-select --install
brew install git
git --version

Linux:包管理器

# Debian / Ubuntu
sudo apt update && sudo apt install git
# Fedora / RHEL
sudo dnf install git
# Arch
sudo pacman -S git

日常用发行版自带包就够;确实需要最新版,再考虑官方 PPA 或源码编译。

首次配置:一次配好,长期省事

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --global core.autocrlf input      # Windows 改为 true
git config --global core.quotepath false     # 中文文件名正常显示
git config --global credential.helper manager
git config --global core.editor "code --wait"

验证安装是否真的可用

git --version
git config --list --show-origin      # 看每项配置来自哪个文件
git config --global --list

常见报错与处理

报错处理
git: command not foundPATH 没生效,重开终端;Windows 重装时选对 PATH 选项
Please tell me who you are没配 user.name / user.email,按「首次配置」补上
warning: LF will be replaced by CRLF换行符设置不一致,统一 core.autocrlf(Windows=true,mac/Linux=input)
中文文件名显示成 \344\270\255git config --global core.quotepath false
Permission denied (publickey)SSH 密钥没配好,见《平台实战》的 SSH 章节

常见问题

Git 和 GitHub Desktop 是一回事吗?

不是。Git 是版本控制工具本身;GitHub Desktop 是图形客户端,底层调用 Git。装 Git 是必须的,图形客户端可选。

装完 Git 一定要配 user.name / user.email 吗?

要。每次提交都会记录作者身份,没配就会报 Please tell me who you are。名字随意,邮箱建议与托管平台账号一致,提交才会正确归属到你。

Windows 上 Git Bash 和 PowerShell 用哪个?

都行。Git Bash 自带类 Unix 环境和常用命令,跑本文的 shell 示例最省心;PowerShell 适合习惯 Windows 的人,注意引号与路径写法差异。选一个顺手的一直用即可。