安装 Git 与首次配置
15 分钟
安装 Git 与首次配置
Git 是跨平台命令行工具。装好后只需配置一次身份,之后在每个仓库里都能提交。
学习目标
- 在 Windows / macOS / Linux 安装 Git
- 配置
user.name与user.email(会写入每次 commit) - 用
git --version验证安装
安装
Windows
- 下载 Git for Windows
- 安装时默认选项即可;勾选 Git Bash 方便执行本教程命令
- 打开 Git Bash 或 PowerShell,执行:
git --version
应看到类似 git version 2.x.x。
macOS
# 已装 Xcode Command Line Tools 通常自带 git
git --version
# 或通过 Homebrew
brew install git
Linux(Debian/Ubuntu)
sudo apt update
sudo apt install -y git
git --version
首次配置(必做)
Git 不会猜你是谁,commit 里需要作者信息:
git config --global user.name "你的昵称或姓名"
git config --global user.email "you@example.com"
建议使用与 GitHub / 公司 Git 账号一致的邮箱,便于网页上关联 commit。
查看当前配置:
git config --global --list
推荐的可选配置
# 默认分支名 main(新项目更常见)
git config --global init.defaultBranch main
# Windows 路径换行符:checkout 时转 LF,避免团队协作 diff 噪音
git config --global core.autocrlf true # Windows
# macOS/Linux 通常:
# git config --global core.autocrlf input
# 默认编辑器(任选)
git config --global core.editor "code --wait"
--global 表示写入用户主目录的 ~/.gitconfig,对本机所有仓库生效。单个仓库可去掉 --global 只在该项目内覆盖。
典型 Web 项目长什么样
一个全栈项目常见结构:
my-app/
├── frontend/ # 学员端或管理端
├── backend/ # API 服务
├── docs/ # 文档
└── .gitignore # 忽略构建产物
在已有项目根目录执行 git status,应能看到 Git 已识别整个仓库。本 track 后续会在单独练习目录里 git init,避免误改他人项目。
常见问题
Q:git 不是内部或外部命令
→ 安装后重开终端;Windows 确认安装路径已加入 PATH。
Q:email 必须真实吗?
→ 公开仓库会显示在 commit 历史里,建议用常用邮箱;GitHub 提供 no-reply 邮箱保护隐私。
Q:公司项目和个人项目用不同身份? → 在公司仓库目录执行不带 的 即可覆盖。