Chapter 02
安装与配置
在正式使用 Git 之前,需要正确安装并进行必要的配置。本章将带你完成 Git 的安装、用户信息设置、SSH 密钥配置以及 .gitignore 的基本用法。
1. 安装 Git
怎么安装 Git?各平台有什么注意事项?
Windows
访问 git-scm.com 下载安装程序。安装过程中务必勾选 "Add Git to PATH",这样可以在任意终端中使用 Git 命令。建议选择默认编辑器(如 VS Code)作为 Git 的默认编辑器。
macOS
两种方式:
# 方式一:Xcode 命令行工具(自带 Git) xcode-select --install # 方式二:Homebrew(推荐,版本更新) brew install git
Linux
# Debian/Ubuntu sudo apt install git # Fedora/RHEL sudo yum install git
验证安装
安装完成后,在终端运行 git --version,看到版本号即表示安装成功。 2. 配置 Git
git config 有哪三个级别?
Git 的配置分为三个级别,优先级从低到高:
system < global < local ┌──────────────┐ ┌─────────────────┐ ┌───────────────┐ │ /etc/gitconfig│ │ ~/.gitconfig │ │ .git/config │ │ 所有用户生效 │ │ 当前用户生效 │ │ 当前仓库生效 │ └──────────────┘ └─────────────────┘ └───────────────┘
system— 系统级配置,对所有用户生效,文件位于/etc/gitconfig(Windows 为C:\Program Files\Git\etc\gitconfig)global— 用户级配置,对当前用户所有仓库生效,文件位于~/.gitconfiglocal— 仓库级配置,仅对当前仓库生效,文件位于.git/config
优先级:local 覆盖 global 覆盖 system。日常使用中,大部分配置都设在 global 级别。
安装后必须配置哪些信息?
至少需要配置以下三项:
# 设置用户名(提交记录中显示的名字) git config --global user.name "你的名字" # 设置邮箱(提交记录中显示的邮箱) git config --global user.email "your@email.com" # 设置默认分支名为 main git config --global init.defaultBranch main
为什么需要配置?
每次提交(commit)都会记录你的名字和邮箱,这样团队成员可以知道每次提交是谁做的。init.defaultBranch 则决定 git init 时默认分支的名称,现代项目推荐使用 main 替代旧的 master。 欢迎来到 Git 配置练习终端!
按照步骤依次设置你的 Git 配置。输入 help 查看可用命令。
$
3. SSH 密钥配置
SSH 和 HTTPS 有什么区别?
| 特性 | SSH | HTTPS |
|---|---|---|
| 认证方式 | 公钥/私钥对 | 用户名 + Token |
| 克隆地址 | git@github.com:user/repo.git | https://github.com/user/repo.git |
| 每次推送 | 无需输入密码 | 需要 Token(可缓存) |
| 配置难度 | 需生成密钥并添加到 GitHub | 开箱即用 |
推荐
如果你经常与 GitHub/GitLab 交互,建议配置 SSH 密钥,一劳永逸。HTTPS 方式在 2021 年后已不支持密码认证,需使用 Personal Access Token(PAT)。 怎么配置 SSH Key?
完整步骤如下:
# 步骤 1:生成 SSH 密钥对(推荐 ed25519 算法) ssh-keygen -t ed25519 -C "your@email.com" # 按三次回车使用默认路径,可设置密码短语(passphrase) # 步骤 2:启动 ssh-agent eval "$(ssh-agent -s)" # 步骤 3:将密钥添加到 agent ssh-add ~/.ssh/id_ed25519 # 步骤 4:复制公钥到剪贴板 # macOS: pbcopy < ~/.ssh/id_ed25519.pub # Windows (Git Bash): cat ~/.ssh/id_ed25519.pub | clip # Linux: cat ~/.ssh/id_ed25519.pub | xclip -sel clip # 步骤 5:添加到 GitHub # 打开 GitHub → Settings → SSH and GPG keys → New SSH key # 粘贴公钥内容,保存 # 步骤 6:测试连接 ssh -T git@github.com # 成功会显示: Hi username! You've successfully authenticated.
4. .gitignore 与常见问题
.gitignore 怎么写?
.gitignore 文件告诉 Git 哪些文件不需要跟踪。常用通配符规则:
# 注释以 # 开头 *.log # 忽略所有 .log 文件 node_modules/ # 忽略整个目录 !important.log # ! 取反,不忽略此文件(即使上面忽略了 *.log) build/*.js # 只忽略 build 目录下的 .js 文件 dist/**/temp # 忽略 dist 下所有子目录中的 temp
两种使用方式:
- 项目级 — 在项目根目录创建
.gitignore,仅对当前项目生效 - 全局级 —
git config --global core.excludesfile ~/.gitignore_global,对所有项目生效
小贴士
GitHub 维护了一份常用 .gitignore 模板集合(github.com/github/gitignore),涵盖了大多数语言和框架,可以直接参考使用。 CRLF 和 LF 换行符问题怎么解决?
不同操作系统使用不同的换行符:
- Windows 使用
CRLF(\r\n) - Linux/macOS 使用
LF(\n)
混合使用会导致 diff 中出现大量无意义的换行符变更。建议配置:
# Windows 用户(检出时转为 CRLF,提交时转为 LF) git config --global core.autocrlf true # macOS/Linux 用户(提交时转为 LF,检出时不转换) git config --global core.autocrlf input
中文文件名显示乱码怎么办?
在 Windows 上,git status 和 git log 可能会将中文文件名显示为形如 \344\270\255\346\226\207 的转义序列。解决方法:
git config --global core.quotepath false
设置后,中文文件名可以正常显示。
欢迎来到 .gitignore 配置练习!
你可以创建文件、编写 .gitignore 规则,然后用 git status 查看效果。
$
