Git 安装与配置
本章将指导你在不同操作系统上安装 Git,并进行基本配置,为后续学习做好准备。
Git 安装
Windows 系统安装
方法一:官方安装包(推荐)
下载安装包
- 访问 Git 官网
- 下载适合你系统的版本(32位或64位)
安装步骤
1. 双击下载的 .exe 文件 2. 选择安装路径(建议使用默认路径) 3. 选择组件(建议保持默认选择) 4. 选择开始菜单文件夹 5. 选择默认编辑器(推荐 VS Code 或 Notepad++) 6. 调整 PATH 环境变量(选择 "Git from the command line and also from 3rd-party software") 7. 选择 HTTPS 传输后端(使用默认的 OpenSSL) 8. 配置行尾转换(选择 "Checkout Windows-style, commit Unix-style line endings") 9. 配置终端模拟器(选择 "Use MinTTY") 10. 配置额外选项(保持默认) 11. 完成安装验证安装
bash# 打开命令提示符或 PowerShell git --version
方法二:包管理器安装
powershell
# 使用 Chocolatey
choco install git
# 使用 Scoop
scoop install git
# 使用 Winget
winget install Git.GitmacOS 系统安装
方法一:Homebrew(推荐)
bash
# 安装 Homebrew(如果还没有安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 安装 Git
brew install git
# 验证安装
git --version方法二:官方安装包
- 访问 Git 官网
- 下载 .dmg 文件
- 双击安装包并按照提示安装
方法三:Xcode Command Line Tools
bash
# 安装 Xcode 命令行工具(包含 Git)
xcode-select --installLinux 系统安装
Ubuntu/Debian
bash
# 更新包列表
sudo apt update
# 安装 Git
sudo apt install git
# 验证安装
git --versionCentOS/RHEL/Fedora
bash
# CentOS/RHEL 7
sudo yum install git
# CentOS/RHEL 8+ 或 Fedora
sudo dnf install git
# 验证安装
git --versionArch Linux
bash
# 安装 Git
sudo pacman -S git
# 验证安装
git --version从源码编译(适用于所有 Linux 发行版)
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 --versionGit 基本配置
安装完成后,需要进行一些基本配置才能正常使用 Git。
配置用户信息
这是使用 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注意事项:
- 用户名可以是中文或英文
- 邮箱建议使用真实邮箱,特别是要与 GitHub 等平台保持一致
--global参数表示全局配置,对所有仓库生效
配置默认编辑器
Git 在某些操作时需要打开编辑器,可以配置你喜欢的编辑器:
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"配置行尾处理
不同操作系统使用不同的行尾字符,Git 可以自动处理这个问题:
bash
# Windows 系统推荐配置
git config --global core.autocrlf true
# macOS/Linux 系统推荐配置
git config --global core.autocrlf input
# 如果团队都使用相同系统,可以关闭自动转换
git config --global core.autocrlf false配置默认分支名
从 Git 2.28 开始,可以配置默认分支名:
bash
# 设置默认分支名为 main
git config --global init.defaultBranch main配置别名
为常用命令设置别名可以提高效率:
bash
# 常用别名配置
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'
# 美化日志显示
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"使用别名:
bash
git st # 等同于 git status
git co main # 等同于 git checkout main
git lg # 美化的日志显示配置颜色输出
让 Git 输出更加美观:
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 有三个配置级别,优先级从高到低:
1. 仓库级配置(--local)
bash
# 只对当前仓库生效
git config --local user.name "项目专用名称"
# 配置文件位置:.git/config2. 用户级配置(--global)
bash
# 对当前用户的所有仓库生效
git config --global user.name "全局用户名"
# 配置文件位置:
# Windows: C:\Users\用户名\.gitconfig
# macOS/Linux: ~/.gitconfig3. 系统级配置(--system)
bash
# 对系统所有用户生效(需要管理员权限)
sudo git config --system core.editor vim
# 配置文件位置:
# Windows: C:\Program Files\Git\etc\gitconfig
# macOS/Linux: /etc/gitconfig查看和管理配置
查看配置
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
# 编辑全局配置文件
git config --global --edit
# 编辑仓库配置文件
git config --local --edit删除配置
bash
# 删除特定配置
git config --global --unset user.name
# 删除整个配置段
git config --global --remove-section aliasSSH 密钥配置
为了安全地与远程仓库通信,建议配置 SSH 密钥:
1. 生成 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"
# 按提示操作:
# 1. 选择保存位置(默认即可)
# 2. 设置密码(可选,但推荐)2. 添加密钥到 SSH 代理
bash
# 启动 SSH 代理
eval "$(ssh-agent -s)"
# 添加私钥到代理
ssh-add ~/.ssh/id_ed255193. 复制公钥
bash
# Linux/macOS
cat ~/.ssh/id_ed25519.pub
# Windows (Git Bash)
cat ~/.ssh/id_ed25519.pub
# Windows (PowerShell)
Get-Content ~/.ssh/id_ed25519.pub4. 添加公钥到 GitHub
- 登录 GitHub
- 点击右上角头像 → Settings
- 左侧菜单选择 "SSH and GPG keys"
- 点击 "New SSH key"
- 粘贴公钥内容
- 点击 "Add SSH key"
5. 测试连接
bash
# 测试 GitHub 连接
ssh -T git@github.com
# 成功的话会看到类似信息:
# Hi username! You've successfully authenticated, but GitHub does not provide shell access.常见配置示例
完整的初始配置脚本
bash
#!/bin/bash
# 基本用户信息
git config --global user.name "张三"
git config --global user.email "zhangsan@example.com"
# 编辑器配置
git config --global core.editor "code --wait"
# 行尾处理(根据系统选择)
git config --global core.autocrlf input # macOS/Linux
# git config --global core.autocrlf true # Windows
# 默认分支名
git config --global init.defaultBranch main
# 颜色输出
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 --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
# 推送配置
git config --global push.default simple
# 拉取配置
git config --global pull.rebase false
echo "Git 配置完成!"验证配置
配置完成后,验证一下设置是否正确:
bash
# 检查用户信息
git config --global user.name
git config --global user.email
# 检查编辑器
git config --global core.editor
# 查看所有配置
git config --list
# 测试 Git 是否正常工作
git --version故障排除
常见问题
命令找不到
bash# 检查 Git 是否在 PATH 中 which git # macOS/Linux where git # Windows权限问题
bash# 检查配置文件权限 ls -la ~/.gitconfig编码问题
bash# 配置 UTF-8 编码 git config --global core.quotepath false git config --global gui.encoding utf-8 git config --global i18n.commit.encoding utf-8 git config --global i18n.logoutputencoding utf-8
总结
完成本章学习后,你应该已经:
- ✅ 在你的系统上成功安装了 Git
- ✅ 配置了基本的用户信息
- ✅ 设置了合适的编辑器和别名
- ✅ 了解了不同级别的配置
- ✅ 可选:配置了 SSH 密钥
这些配置将为你后续使用 Git 提供良好的基础。在下一章中,我们将通过一个快速上手的例子来体验 Git 的基本功能。