Skip to content

Git 安装

概述

Git 是一个免费、开源的分布式版本控制系统,在开始使用 Git 之前,需要先在你的操作系统上安装它。本章将详细介绍在不同操作系统上安装 Git 的方法,以及如何验证安装是否成功。

Git 支持所有主流操作系统,包括 Windows、macOS 和 Linux。不同系统的安装方式略有不同,但安装后的使用方法完全一致。

Windows 安装方法

方法一:官方安装包(推荐)

下载安装:

  1. 访问 Git 官方网站:https://git-scm.com/download/win
  2. 下载最新版本的安装包(自动识别系统架构)
  3. 运行安装程序,按照向导完成安装

安装步骤详解:

powershell
# 安装过程中的重要选项:

# 1. 选择安装位置(建议默认)
Destination Location: C:\Program Files\Git

# 2. 选择组件(建议全选)
[x] Git Bash Here
[x] Git GUI Here
[x] Associate .git* configuration files
[x] Associate .sh files to be run with Bash

# 3. 选择开始菜单文件夹
Start Menu Folder: Git

# 4. 选择默认编辑器(推荐 VS Code)
Choose Git's default editor: Visual Studio Code

# 5. 调整 PATH 环境(推荐第二个选项)
[x] Git from the command line and also from 3rd-party software

# 6. 选择 SSH 可执行文件
[x] Use bundled OpenSSH

# 7. 选择 HTTPS 传输后端
[x] Use the OpenSSL library

# 8. 配置行尾转换
[x] Checkout Windows-style, commit Unix-style line endings

# 9. 配置终端模拟器
[x] Use MinTTY (the default terminal of MSYS2)

# 10. 配置 git pull 行为
[x] Default (fast-forward or merge)

# 11. 配置凭据管理器
[x] Git Credential Manager

# 12. 配置额外选项
[x] Enable file system caching
[x] Enable symbolic links

方法二:使用 winget(Windows 10/11)

powershell
# 使用 Windows 包管理器安装
winget install --id Git.Git -e --source winget

# 安装完成后重启终端

方法三:使用 Chocolatey

powershell
# 先安装 Chocolatey(如果未安装)
Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

# 安装 Git
choco install git -y

# 安装完成后重启终端

方法四:使用 Scoop

powershell
# 安装 Scoop(如果未安装)
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
irm get.scoop.sh | iex

# 安装 Git
scoop install git

# 安装 Git 凭据管理器
scoop install git-credential-manager-core

Windows 安装后配置

powershell
# 打开 Git Bash 或 PowerShell

# 配置用户名和邮箱
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# 配置默认编辑器
git config --global core.editor "code --wait"

# 配置换行符处理
git config --global core.autocrlf true

# 配置文件名大小写敏感
git config --global core.ignorecase false

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

macOS 安装方法

方法一:Xcode Command Line Tools(推荐)

bash
# 安装 Xcode Command Line Tools(包含 Git)
xcode-select --install

# 弹出对话框后点击"安装"
# 安装完成后验证
git --version
# 输出: git version 2.x.x

方法二:使用 Homebrew(推荐)

bash
# 安装 Homebrew(如果未安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# 安装 Git
brew install git

# 验证安装
git --version

# 查看 Git 安装路径
which git
# 输出: /opt/homebrew/bin/git (Apple Silicon)
# 或: /usr/local/bin/git (Intel)

方法三:使用官方安装包

bash
# 下载地址
# https://git-scm.com/download/mac

# 下载 .dmg 文件后双击安装
# 按照安装向导完成安装

方法四:使用 MacPorts

bash
# 安装 MacPorts(如果未安装)
# 参考: https://www.macports.org/install.php

# 安装 Git
sudo port install git

# 验证安装
git --version

macOS 安装后配置

bash
# 配置用户名和邮箱
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# 配置默认编辑器
git config --global core.editor "code --wait"
# 或使用 vim
git config --global core.editor "vim"

# 配置换行符处理(macOS)
git config --global core.autocrlf input

# 配置大小写敏感(macOS 默认不敏感)
git config --global core.ignorecase false

# 配置 Keychain 凭据存储
git config --global credential.helper osxkeychain

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

Linux 安装方法

Debian/Ubuntu

bash
# 更新包列表
sudo apt update

# 安装 Git
sudo apt install git -y

# 验证安装
git --version

# 安装额外依赖(可选)
sudo apt install git-doc git-el git-email git-gui gitk gitweb git-cvs git-mediawiki git-svn -y

Fedora

bash
# 安装 Git
sudo dnf install git -y

# 验证安装
git --version

CentOS/RHEL

bash
# CentOS 7
sudo yum install git -y

# CentOS 8 / RHEL 8
sudo dnf install git -y

# 验证安装
git --version

Arch Linux

bash
# 安装 Git
sudo pacman -S git

# 验证安装
git --version

openSUSE

bash
# 安装 Git
sudo zypper install git

# 验证安装
git --version

Alpine Linux

bash
# 安装 Git
apk add git

# 验证安装
git --version

从源码编译安装(获取最新版本)

bash
# 安装编译依赖
sudo apt update
sudo apt install build-essential libssl-dev libcurl4-openssl-dev libexpat1-dev gettext unzip -y

# 下载最新版本(示例:2.43.0)
cd /tmp
wget https://github.com/git/git/archive/refs/tags/v2.43.0.tar.gz
tar -xzf v2.43.0.tar.gz
cd git-2.43.0

# 编译安装
make prefix=/usr/local all
sudo make prefix=/usr/local install

# 验证安装
git --version
# 输出: git version 2.43.0

Linux 安装后配置

bash
# 配置用户名和邮箱
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# 配置默认编辑器
git config --global core.editor vim
# 或使用 nano
git config --global core.editor nano

# 配置换行符处理
git config --global core.autocrlf input

# 配置凭据缓存
git config --global credential.helper cache
# 或使用 libsecret(需要安装)
git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret

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

验证安装

检查 Git 版本

bash
# 查看 Git 版本
git --version
# 输出: git version 2.43.0

# 查看详细版本信息
git version

检查 Git 安装路径

bash
# 查看 Git 可执行文件路径
which git
# Linux/macOS: /usr/bin/git 或 /usr/local/bin/git
# Windows (Git Bash): /usr/bin/git

# Windows PowerShell
Get-Command git

检查 Git 配置

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

# 查看系统配置
git config --system --list

# 查看全局配置
git config --global --list

# 查看特定配置项
git config user.name
git config user.email
git config core.editor

运行首次设置检查

bash
# 检查 Git 是否正常工作
git init test-repo
cd test-repo
echo "# Test" > README.md
git add README.md
git commit -m "Initial commit"
git log --oneline
# 输出: abc1234 Initial commit

# 清理测试仓库
cd ..
rm -rf test-repo

检查 SSH 连接(可选)

bash
# 测试 SSH 连接(如果使用 SSH)
ssh -T git@github.com
# 输出: Hi username! You've successfully authenticated...

# 测试 GitLab SSH
ssh -T git@gitlab.com

# 测试 Gitee SSH
ssh -T git@gitee.com

常见安装问题

问题 1:命令未找到

bash
# 错误信息
git: command not found

# 解决方案

# 1. 确认 Git 已安装
which git

# 2. 检查 PATH 环境变量
echo $PATH

# 3. 添加 Git 到 PATH(Linux/macOS)
export PATH="/usr/local/git/bin:$PATH"
# 添加到 ~/.bashrc 或 ~/.zshrc
echo 'export PATH="/usr/local/git/bin:$PATH"' >> ~/.bashrc

# 4. Windows: 重新安装,确保选择添加到 PATH

问题 2:版本过旧

bash
# 查看当前版本
git --version
# 输出: git version 2.25.1

# 查看最新版本
# 访问: https://git-scm.com/downloads

# 升级 Git

# macOS
brew upgrade git

# Ubuntu(添加官方 PPA)
sudo add-apt-repository ppa:git-core/ppa
sudo apt update
sudo apt install git

# Windows
# 下载最新安装包覆盖安装

问题 3:权限问题

bash
# 错误信息
error: could not lock config file ... Permission denied

# 解决方案

# Linux/macOS: 检查文件权限
ls -la ~/.gitconfig
# 应该是当前用户所有

# 修复权限
sudo chown -R $(whoami) ~/.gitconfig
sudo chown -R $(whoami) ~/.config/git

# Windows: 以管理员身份运行

问题 4:SSL 证书问题

bash
# 错误信息
SSL certificate problem: unable to get local issuer certificate

# 解决方案

# 方法 1:更新 CA 证书(推荐)
# macOS
brew install ca-certificates

# Ubuntu
sudo apt install ca-certificates

# 方法 2:临时禁用 SSL 验证(不推荐)
git config --global http.sslVerify false

# 方法 3:指定 CA 证书路径
git config --global http.sslCAInfo /path/to/certificate.crt

问题 5:Windows 换行符问题

bash
# 错误信息
warning: LF will be replaced by CRLF

# 解决方案

# 配置换行符自动转换
git config --global core.autocrlf true

# 或配置 .gitattributes 文件
cat > .gitattributes << 'EOF'
* text=auto
*.php text eol=lf
*.sh text eol=lf
*.bat text eol=crlf
EOF

安装 GUI 工具(可选)

跨平台 GUI 工具

bash
# VS Code Git 集成(推荐)
# 安装 VS Code 后自动集成 Git 功能

# Sourcetree(免费)
# https://www.sourcetreeapp.com/

# GitKraken(部分免费)
# https://www.gitkraken.com/

# GitHub Desktop
# https://desktop.github.com/

# Sublime Merge
# https://www.sublimemerge.com/

Linux GUI 工具

bash
# GitKraken
# 下载: https://www.gitkraken.com/download

# Git Cola
sudo apt install git-cola

# Gitg
sudo apt install gitg

# SmartGit
# 下载: https://www.syntevo.com/smartgit/

macOS GUI 工具

bash
# Sourcetree
brew install --cask sourcetree

# GitKraken
brew install --cask gitkraken

# GitHub Desktop
brew install --cask github

# Tower
brew install --cask tower

总结

安装方式对比

操作系统推荐方式备选方式
Windows官方安装包winget, Chocolatey, Scoop
macOSHomebrewXcode Command Line Tools
Linux包管理器源码编译

安装后必做事项

bash
# 1. 验证安装
git --version

# 2. 配置用户信息
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# 3. 配置编辑器
git config --global core.editor "code --wait"

# 4. 验证配置
git config --global --list

# 5. 测试 Git 功能
git init test-repo && cd test-repo
echo "# Test" > README.md
git add . && git commit -m "test"
git log

下一步

安装完成后,建议继续学习: