git-install

Updated: 2026-04-19

Git 安装与配置

三大操作系统的安装指南 + 安装后必做的首次配置。


目录

  1. Windows 安装
  2. macOS 安装
  3. Linux 安装
  4. 验证安装
  5. 首次配置(必做)
  6. 配置作用域详解
  7. 推荐的可选配置

1. Windows 安装

下载

访问 Git 官网,下载最新版安装包(.exe 文件)。

安装步骤

双击运行安装程序,以下是各步骤的推荐设置:

安装界面 推荐选项 说明
Select Components 勾选 Git Bash HereGit GUI Here 右键菜单集成,非常方便
Default Editor 选择你熟悉的编辑器 推荐 VS Code(Use Visual Studio Code as Git's default editor
Initial Branch Name Let Git decideOverride: main 新仓库的默认分支名
PATH environment Git from the command line and also from 3rd-party software 让 CMD/PowerShell 也能用 git 命令
SSH executable Use bundled OpenSSH 使用 Git 自带的 SSH
HTTPS transport backend Use the OpenSSL library 默认即可
Line ending conversions Checkout Windows-style, commit Unix-style line endings 重要:解决跨系统换行符问题
Terminal emulator Use MinTTY Git Bash 的终端,比 CMD 好用
Default behavior of git pull Fast-forward or merge 默认即可
Credential helper Git Credential Manager 自动管理 HTTPS 凭证
其余选项 保持默认

验证

安装完成后,右键桌面空白处,如果能看到 Git Bash HereGit GUI Here 菜单项,说明安装成功。

打开 Git Bash,输入:

git --version
# 输出示例:git version 2.44.0.windows.1

2. macOS 安装

方法一:Homebrew(推荐)

如果已安装 Homebrew

brew install git

方法二:Xcode Command Line Tools

xcode-select --install

弹出对话框后点击「安装」即可。这会安装 Apple 维护的 Git 版本(通常略旧于最新版)。

方法三:官网安装包

访问 git-scm.com 下载 .dmg 安装包。


3. Linux 安装

Debian / Ubuntu

sudo apt update
sudo apt install git

Fedora

sudo dnf install git

CentOS / RHEL

# CentOS 7
sudo yum install git

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

Arch Linux

sudo pacman -S git

openSUSE

sudo zypper install git

4. 验证安装

无论哪个平台,安装完成后在终端运行:

git --version

输出类似以下内容即为成功:

git version 2.44.0

如果提示 command not found,说明 Git 没有正确加入系统 PATH。尝试重启终端或重新安装。


5. 首次配置(必做)

安装好 Git 后,必须设置你的身份信息。这些信息会写入你每次 commit 的记录中。

# 设置用户名(建议和 GitHub 用户名一致)
git config --global user.name "你的名字"

# 设置邮箱(建议和 GitHub 注册邮箱一致)
git config --global user.email "your.email@example.com"

为什么这很重要?

Git 用这两个字段来标记每次提交的作者。如果你以后要用 GitHub,邮箱必须和 GitHub 账号的邮箱对应,否则 GitHub 无法正确关联你的提交。

验证配置

git config --global user.name
# 输出:你的名字

git config --global user.email
# 输出:your.email@example.com

# 查看所有 global 配置
git config --global --list

6. 配置作用域详解

Git 的配置有三个层级,优先级从高到低:

作用域 参数 配置文件位置 适用范围
仓库级 无参数 .git/config(仓库内) 仅当前仓库
用户级 --global ~/.gitconfig 当前用户的所有仓库
系统级 --system /etc/gitconfig 所有用户的所有仓库
# 仓库级(优先级最高)— 只影响当前仓库
git config user.name "工作用名字"

# 用户级 — 影响你所有仓库
git config --global user.name "你的名字"

# 系统级(优先级最低)— 很少用
git config --system core.autocrlf true

实际应用场景

如果你有个人项目和公司项目,可以:

  • --global 设置个人信息
  • 在公司项目仓库内用无参数版本覆盖为公司信息
# 全局配置:个人身份
git config --global user.name "个人名字"
git config --global user.email "personal@email.com"

# 在公司项目目录下单独配置
cd ~/work/company-project
git config user.name "公司用名字"
git config user.email "work@company.com"

7. 推荐的可选配置

以下配置不是必须的,但强烈推荐:

默认编辑器

# VS Code(推荐)
git config --global core.editor "code --wait"

# Vim
git config --global core.editor "vim"

# Nano
git config --global core.editor "nano"

# Notepad++(Windows)
git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

默认分支名

# 将新仓库的默认分支名设为 main(推荐,GitHub 默认已改为 main)
git config --global init.defaultBranch main

换行符处理

# Windows 用户(签出时 LF→CRLF,提交时 CRLF→LF)
git config --global core.autocrlf true

# Mac/Linux 用户(提交时 CRLF→LF,签出不转换)
git config --global core.autocrlf input

中文支持

# 正确显示中文文件名(默认会被转义为 \xxx 格式)
git config --global core.quotepath false

颜色输出

# 通常默认已开启
git config --global color.ui auto

常用别名(可选)

# 简写常用命令
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.lg "log --oneline --graph --all --decorate"

设置后即可使用:

git st        # 等同于 git status
git co main   # 等同于 git checkout main
git lg        # 美观的图形化日志

最终检查

# 查看你的所有 global 配置
git config --global --list

# 输出示例:
# user.name=你的名字
# user.email=your.email@example.com
# core.editor=code --wait
# init.defaultbranch=main
# core.autocrlf=true
# core.quotepath=false
# alias.st=status
# alias.lg=log --oneline --graph --all --decorate

延伸阅读


最后更新:2026-04-19

目录