Skip to content
On this page

Git 安装与配置

本章详细介绍Git的安装方法和各种配置选项,帮助您正确设置Git环境。

Git安装

Windows系统安装

方法一:官方安装包

  1. 访问Git官网
  2. 点击"Download for Windows"
  3. 运行下载的安装程序

安装过程中的重要选项:

  • 选择组件:建议勾选所有默认选项
  • 选择开始菜单文件夹:保持默认设置
  • 选择默认编辑器:建议选择"Use Visual Studio Code as Git's default editor"
  • 调整PATH环境:选择"Git from the command line and also from 3rd-party software"
  • 选择HTTPS传输后端:选择"OpenSSL library"
  • 配置行结束符转换:选择"Checkout Windows-style, commit Unix-style line endings"
  • 配置Git Bash终端模拟器:选择"Use MinTTY"
  • 配置额外选项:勾选"Enable file system caching"和"Enable Git Credential Manager"

方法二:使用包管理器

bash
# 使用Chocolatey
choco install git

# 使用Scoop
scoop install git

macOS系统安装

方法一:使用Homebrew

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

# 安装Git
brew install git

方法二:使用Xcode命令行工具

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

方法三:官方安装包

  1. 访问Git官网
  2. 下载macOS版本的安装包
  3. 运行安装程序

Linux系统安装

基于Debian/Ubuntu

bash
# 更新包列表
sudo apt update

# 安装Git
sudo apt install git

# 验证安装
git --version

基于Red Hat/CentOS/Fedora

bash
# 对于CentOS/RHEL
sudo yum install git

# 对于较新版本的Fedora
sudo dnf install git

# 验证安装
git --version

基于Arch Linux

bash
sudo pacman -S git

Git配置

配置级别

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

  1. local:仓库级配置(.git/config
  2. global:用户级配置(~/.gitconfig
  3. system:系统级配置(/etc/gitconfig

基本用户信息配置

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

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

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

编辑器配置

bash
# 配置默认编辑器
git config --global core.editor "code --wait"  # VS Code
git config --global core.editor "vim"         # Vim
git config --global core.editor "subl -n -w"  # Sublime Text

行结束符配置

bash
# 自动转换行结束符(Windows推荐)
git config --global core.autocrlf true

# 不转换行结束符(Unix/Linux/macOS推荐)
git config --global core.autocrlf input

# 完全不转换(纯Git仓库)
git config --global core.autocrlf false

文件名大小写敏感性

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

# 忽略文件名大小写(Windows用户可能需要)
git config --global core.ignorecase true

提交模板配置

bash
# 创建提交模板
echo "# 提交信息模板
# 
# 1. 第一行是简短的摘要(50字符以内)
# 2. 空一行
# 3. 详细描述(72字符以内每行)
" > ~/.git-commit-template

# 配置提交模板
git config --global commit.template ~/.git-commit-template

高级配置选项

颜色配置

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

# 详细配置各部分颜色
git config --global color.diff.meta "blue black bold"
git config --global color.diff.frag "magenta bold"
git config --global color.diff.old "red bold"
git config --global color.diff.new "green bold"

别名配置

bash
# 创建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.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual '!gitk'

推送策略配置

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

# 其他选项:
# - nothing:不推送任何内容
# - current:推送当前分支
# - upstream:推送到上游分支
# - simple:简单模式(当前分支与上游分支同名时)

拉取策略配置

bash
# 配置拉取时的合并策略
git config --global pull.rebase false  # 使用merge(默认)
git config --global pull.rebase true   # 使用rebase

安全配置

凭证助手

bash
# 使用凭证助手存储密码
git config --global credential.helper cache        # 临时存储(默认15分钟)
git config --global credential.helper store       # 永久存储(不推荐)
git config --global credential.helper 'cache --timeout=3600'  # 自定义超时时间

# Windows系统使用Credential Manager
git config --global credential.helper manager

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

# Linux使用libsecret
git config --global credential.helper libsecret

SSH配置

bash
# 生成SSH密钥
ssh-keygen -t rsa -b 4096 -C "your.email@example.com"

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

# 添加SSH密钥到ssh-agent
ssh-add ~/.ssh/id_rsa

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

GPG签名配置

bash
# 生成GPG密钥
gpg --full-generate-key

# 查看GPG密钥
gpg --list-secret-keys --keyid-format LONG

# 配置Git使用GPG签名
git config --global user.signingkey <your-gpg-key-id>

# 启用提交签名
git config --global commit.gpgsign true

# 或者为特定仓库启用
git config commit.gpgsign true

性能优化配置

大文件支持

bash
# 安装Git LFS(Large File Storage)
# 首先安装Git LFS
git lfs install

# 跟踪特定类型的大文件
git lfs track "*.psd"
git lfs track "*.zip"
git lfs track "*.iso"

压缩配置

bash
# 设置压缩级别(0-9,9为最高压缩)
git config --global core.compression 9

# 设置对象数据库压缩
git config --global pack.compression 9

# 设置传输时的压缩
git config --global http.postBuffer 524288000  # 500MB

仓库优化

bash
# 自动垃圾回收配置
git config --global gc.auto 256

# 网络传输优化
git config --global http.postBuffer 1048576000  # 1GB

验证配置

检查配置

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

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

# 验证配置是否正确
git config --get user.name
git config --get user.email

测试安装

bash
# 检查Git版本
git --version

# 检查帮助
git --help

# 初始化测试仓库
mkdir git-test && cd git-test
git init
echo "# Git Test" > README.md
git add README.md
git commit -m "Initial commit"

配置文件管理

配置文件位置

  • 系统级/etc/gitconfig
  • 用户级~/.gitconfig~/.config/git/config
  • 仓库级<repo>/.git/config

配置文件格式

ini
# ~/.gitconfig 示例
[user]
    name = Your Name
    email = your.email@example.com
[core]
    editor = code --wait
    autocrlf = true
[color]
    ui = auto
[alias]
    st = status
    co = checkout
    br = branch
[push]
    default = simple

配置文件备份

bash
# 备份配置文件
cp ~/.gitconfig ~/.gitconfig.backup

# 恢复配置文件
cp ~/.gitconfig.backup ~/.gitconfig

正确配置Git是高效使用Git的前提,建议根据您的工作环境和需求进行相应的配置。