Skip to content

Git 安装与配置

本章将详细介绍如何在不同操作系统上安装 Git,以及如何进行基本配置,让你快速开始使用 Git。

Git 安装

Windows 系统安装

方法一:官方安装包

  1. 访问 Git 官网
  2. 点击 "Download for Windows"
  3. 下载完成后运行安装程序
  4. 按照安装向导完成安装
bash
# 验证安装
git --version

方法二:使用包管理器

bash
# 使用 Chocolatey
choco install git

# 使用 Scoop
scoop install git

# 使用 winget
winget install Git.Git

安装选项说明

  • 调整 PATH 环境:选择 "Git from the command line and also from 3rd-party software"
  • 选择 HTTPS 传输后端:选择 "Use the OpenSSL library"
  • 配置行尾转换:选择 "Checkout Windows-style, commit Unix-style line endings"
  • 配置终端模拟器:选择 "Use Windows' default console window"

macOS 系统安装

方法一:使用 Homebrew(推荐)

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

# 安装 Git
brew install git

# 验证安装
git --version

方法二:使用 MacPorts

bash
sudo port install git

方法三:官方安装包

  1. 访问 Git 官网
  2. 下载 macOS 版本
  3. 运行 .dmg 文件并按照提示安装

方法四:Xcode Command Line Tools

bash
# 安装 Xcode 命令行工具(包含 Git)
xcode-select --install

Linux 系统安装

Ubuntu/Debian 系统

bash
# 更新包列表
sudo apt update

# 安装 Git
sudo apt install git

# 验证安装
git --version

CentOS/RHEL/Fedora 系统

bash
# CentOS/RHEL 7
sudo yum install git

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

# 验证安装
git --version

Arch Linux

bash
# 安装 Git
sudo pacman -S git

# 验证安装
git --version

从源码编译安装

bash
# 安装依赖
sudo apt install make libssl-dev libghc-zlib-dev libcurl4-gnutls-dev libreadline-dev autoconf build-essential gettext libtool

# 下载源码
wget https://github.com/git/git/archive/v2.40.0.tar.gz
tar -xzf v2.40.0.tar.gz
cd git-2.40.0

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

# 验证安装
git --version

Git 基本配置

用户信息配置

安装完成后,首先需要配置用户信息:

bash
# 配置用户名
git config --global user.name "你的姓名"

# 配置邮箱
git config --global user.email "your.email@example.com"

# 查看配置
git config --global user.name
git config --global user.email

配置级别

Git 有三个配置级别:

bash
# 系统级配置(所有用户)
git config --system user.name "System User"

# 全局配置(当前用户)
git config --global user.name "Global User"

# 仓库级配置(当前仓库)
git config --local user.name "Local User"

优先级:local > global > system

查看配置

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

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

# 查看配置来源
git config --list --show-origin

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

# 查看本地配置
git config --local --list

编辑器配置

bash
# 设置默认编辑器为 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"

差异工具配置

bash
# 设置差异查看工具
git config --global diff.tool vimdiff

# 设置为 VS Code
git config --global diff.tool vscode
git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'

# 设置合并工具
git config --global merge.tool vimdiff

别名配置

创建常用命令的别名:

bash
# 基本别名
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 alias.visual '!gitk'

# 美化日志
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

# 查看别名
git config --get-regexp alias

行尾处理配置

bash
# Windows 系统推荐配置
git config --global core.autocrlf true

# macOS/Linux 系统推荐配置
git config --global core.autocrlf input

# 禁用自动转换
git config --global core.autocrlf false

# 设置安全检查
git config --global core.safecrlf true

颜色配置

bash
# 启用颜色输出
git config --global color.ui auto

# 具体配置
git config --global color.branch auto
git config --global color.diff auto
git config --global color.status auto

# 自定义颜色
git config --global color.diff.meta "blue black bold"

推送配置

bash
# 设置推送策略(推荐)
git config --global push.default simple

# 其他推送策略
git config --global push.default current    # 推送当前分支到同名远程分支
git config --global push.default upstream   # 推送到上游分支
git config --global push.default matching   # 推送所有匹配的分支

凭据配置

bash
# Windows 凭据管理器
git config --global credential.helper manager-core

# macOS 钥匙串
git config --global credential.helper osxkeychain

# Linux 缓存凭据
git config --global credential.helper cache

# 设置缓存时间(秒)
git config --global credential.helper 'cache --timeout=3600'

SSH 密钥配置

生成 SSH 密钥

bash
# 生成新的 SSH 密钥
ssh-keygen -t ed25519 -C "your.email@example.com"

# 如果系统不支持 ed25519,使用 RSA
ssh-keygen -t rsa -b 4096 -C "your.email@example.com"

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

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

配置 SSH 密钥

bash
# 查看公钥内容
cat ~/.ssh/id_ed25519.pub

# 复制公钥到剪贴板(macOS)
pbcopy < ~/.ssh/id_ed25519.pub

# 复制公钥到剪贴板(Linux)
xclip -sel clip < ~/.ssh/id_ed25519.pub

# 复制公钥到剪贴板(Windows Git Bash)
clip < ~/.ssh/id_ed25519.pub

测试 SSH 连接

bash
# 测试 GitHub 连接
ssh -T git@github.com

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

# 测试 Bitbucket 连接
ssh -T git@bitbucket.org

配置文件位置

配置文件路径

bash
# 系统级配置文件
# Windows: C:\Program Files\Git\etc\gitconfig
# macOS/Linux: /etc/gitconfig

# 全局配置文件
# Windows: C:\Users\<username>\.gitconfig
# macOS/Linux: ~/.gitconfig

# 仓库级配置文件
# 项目根目录/.git/config

直接编辑配置文件

bash
# 编辑全局配置文件
git config --global --edit

# 编辑本地配置文件
git config --edit

示例 .gitconfig 文件:

ini
[user]
    name = 张三
    email = zhangsan@example.com

[core]
    editor = code --wait
    autocrlf = true
    safecrlf = true

[color]
    ui = auto

[alias]
    co = checkout
    br = branch
    ci = commit
    st = status
    lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

[push]
    default = simple

[credential]
    helper = manager-core

[diff]
    tool = vscode

[difftool "vscode"]
    cmd = code --wait --diff $LOCAL $REMOTE

[merge]
    tool = vscode

[mergetool "vscode"]
    cmd = code --wait $MERGED

代理配置

HTTP/HTTPS 代理

bash
# 设置 HTTP 代理
git config --global http.proxy http://proxy.company.com:8080

# 设置 HTTPS 代理
git config --global https.proxy https://proxy.company.com:8080

# 设置 SOCKS 代理
git config --global http.proxy socks5://127.0.0.1:1080

# 取消代理设置
git config --global --unset http.proxy
git config --global --unset https.proxy

针对特定域名的代理

bash
# 为 GitHub 设置代理
git config --global http.https://github.com.proxy socks5://127.0.0.1:1080

# 取消特定域名代理
git config --global --unset http.https://github.com.proxy

常见配置问题

问题1:中文文件名显示问题

bash
# 解决中文文件名显示为数字的问题
git config --global core.quotepath false

问题2:换行符问题

bash
# 查看当前设置
git config core.autocrlf

# Windows 用户设置
git config --global core.autocrlf true

# macOS/Linux 用户设置
git config --global core.autocrlf input

问题3:大小写敏感问题

bash
# 设置大小写敏感(默认 false)
git config --global core.ignorecase false

问题4:文件权限问题

bash
# 忽略文件权限变化
git config --global core.filemode false

验证配置

创建一个测试脚本来验证配置:

bash
#!/bin/bash
echo "=== Git 配置验证 ==="
echo "Git 版本: $(git --version)"
echo "用户名: $(git config user.name)"
echo "邮箱: $(git config user.email)"
echo "编辑器: $(git config core.editor)"
echo "换行符处理: $(git config core.autocrlf)"
echo "颜色输出: $(git config color.ui)"
echo "推送策略: $(git config push.default)"
echo ""
echo "=== SSH 密钥测试 ==="
if [ -f ~/.ssh/id_ed25519.pub ]; then
    echo "SSH 密钥存在: ~/.ssh/id_ed25519.pub"
else
    echo "SSH 密钥不存在,请生成密钥"
fi
echo ""
echo "=== 别名配置 ==="
git config --get-regexp alias

总结

正确的 Git 安装和配置是高效使用 Git 的基础:

安装要点

  • ✅ 选择适合操作系统的安装方法
  • ✅ 验证安装是否成功
  • ✅ 了解不同安装选项的含义

配置要点

  • ✅ 设置用户名和邮箱(必需)
  • ✅ 配置合适的编辑器
  • ✅ 设置行尾处理策略
  • ✅ 创建有用的别名
  • ✅ 配置 SSH 密钥

最佳实践

  • 🔧 使用全局配置设置通用选项
  • 🔧 为不同项目设置特定的本地配置
  • 🔧 定期备份配置文件
  • 🔧 了解配置的优先级

完成配置后,你就可以开始使用 Git 进行版本控制了。在下一章中,我们将学习 Git 的快速上手指南。

本站内容仅供学习和研究使用。