目录
下载与安装
Windows
官方下载
访问 Git 官网
下载 Windows 安装程序(通常是 64-bit Git for Windows Setup)
运行安装程序,按向导完成安装
使用包管理器
# 使用 Chocolatey choco install git # 使用 Scoop scoop install git # 使用 winget winget install Git.Git验证安装
git --version
macOS
使用 Homebrew(推荐)
brew install git使用 Xcode Command Line Tools
xcode-select --install官方安装包
访问 Git 官网
下载 macOS 安装程序
验证安装
git --version
Linux
Ubuntu/Debian
sudo apt update
sudo apt install git
CentOS/RHEL/Fedora
# CentOS/RHEL
sudo yum install git
# Fedora
sudo dnf install git
Arch Linux
sudo pacman -S git
验证安装
git --version
其他安装方式
从源码编译:访问 Git 官网 获取源码
Docker 镜像:
docker pull alpine/git
初始配置
在开始使用 Git 之前,需要先配置用户名和邮箱。这些信息会记录在你的每次提交中。
设置全局用户名和邮箱(推荐)
# 设置全局用户名
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
设置本地仓库的用户名和邮箱
如果某个仓库需要使用不同的用户名和邮箱,可以设置本地配置(会覆盖全局配置):
# 设置本地用户名(去掉 --global)
git config user.name "Your Name"
# 设置本地邮箱(去掉 --global)
git config user.email "your.email@example.com"
配置优先级
Git 配置有三个级别,优先级从高到低:
本地配置(
.git/config):只对当前仓库有效git config user.name "Local Name"全局配置(
~/.gitconfig或~/.config/git/config):对当前用户的所有仓库有效git config --global user.name "Global Name"系统配置(
/etc/gitconfig):对所有用户有效(需要管理员权限)git config --system user.name "System Name"
查看所有配置
# 查看所有配置
git config --list
# 查看全局配置
git config --global --list
# 查看本地配置
git config --local --list
# 查看系统配置
git config --system --list
修改已存在的配置
# 修改全局用户名
git config --global user.name "New Name"
# 修改全局邮箱
git config --global user.email "new.email@example.com"
删除配置
# 删除全局配置
git config --global --unset user.name
git config --global --unset user.email
# 删除本地配置
git config --unset user.name
git config --unset user.email
完整初始化示例
# 1. 配置用户信息
git config --global user.name "张三"
git config --global user.email "zhangsan@example.com"
# 2. 验证配置
git config --list
# 3. 可选:配置其他常用设置
git config --global init.defaultBranch main
git config --global color.ui auto
git config --global core.editor "code --wait" # VS Code
# 4. 查看 Git 版本
git --version
基础操作
初始化和克隆
# 初始化新仓库
git init
# 克隆远程仓库
git clone <repository-url>
git clone <repository-url> <directory-name> # 指定目录名
# 克隆指定分支
git clone -b <branch-name> <repository-url>
文件状态查看
# 查看工作区和暂存区状态
git status
git status -s # 简短格式
git status -u # 显示未跟踪的文件
# 查看文件差异
git diff # 工作区 vs 暂存区
git diff --staged # 暂存区 vs 最新提交
git diff HEAD # 工作区 vs 最新提交
git diff <commit-id> # 与指定提交对比
添加文件
# 添加文件到暂存区
git add <file> # 添加指定文件
git add . # 添加当前目录所有更改
git add -A # 添加所有更改(包括删除)
git add -u # 只添加已跟踪文件的更改
# 交互式添加
git add -p # 交互式选择要暂存的部分
git add -i # 交互式添加模式
提交
# 提交更改
git commit -m "提交信息"
git commit -m "标题" -m "详细描述" # 多行提交信息
# 跳过暂存区直接提交(已跟踪的文件)
git commit -a -m "提交信息"
# 修改最后一次提交
git commit --amend # 修改提交信息或添加遗漏的文件
git commit --amend --no-edit # 修改提交内容但不改信息
# 空提交(用于触发 CI/CD)
git commit --allow-empty -m "空提交"
分支管理
查看分支
# 查看本地分支
git branch
# 查看所有分支(包括远程)
git branch -a
# 查看远程分支
git branch -r
# 查看分支详细信息
git branch -v # 显示最后提交信息
git branch -vv # 显示跟踪的远程分支
创建和切换分支
# 创建新分支(不切换)
git branch <branch-name>
# 创建并切换到新分支
git checkout -b <branch-name>
git switch -c <branch-name> # Git 2.23+ 新命令
# 切换到已存在的分支
git checkout <branch-name>
git switch <branch-name> # Git 2.23+ 新命令
# 切换到上一个分支
git checkout -
git switch - # Git 2.23+ 新命令
# 基于远程分支创建本地分支
git checkout -b <local-branch> origin/<remote-branch>
删除分支
# 删除本地分支
git branch -d <branch-name> # 安全删除(会检查是否已合并)
git branch -D <branch-name> # 强制删除(未合并也会删除)
# 删除远程分支
git push origin --delete <branch-name>
git push origin :<branch-name> # 旧语法
重命名分支
# 重命名当前分支
git branch -m <new-name>
# 重命名指定分支
git branch -m <old-name> <new-name>
# 更新远程分支名
git branch -m <old-name> <new-name>
git push origin :<old-name> <new-name>
git push origin -u <new-name>
提交历史
查看提交历史
# 基本查看
git log
# 单行显示
git log --oneline
# 图形化显示
git log --graph
git log --graph --oneline --all # 显示所有分支的图形化历史
# 限制显示数量
git log -n <number> # 显示最近 n 条
git log -5 # 显示最近 5 条
# 按作者筛选
git log --author="<author-name>"
# 按时间筛选
git log --since="2024-01-01"
git log --until="2024-12-31"
git log --since="2 weeks ago"
# 按文件筛选
git log -- <file-path>
# 搜索提交信息
git log --grep="关键词"
# 显示统计信息
git log --stat # 显示文件变更统计
git log --shortstat # 简短的统计信息
git log --name-only # 只显示文件名
git log --name-status # 显示文件名和状态(A/M/D)
# 显示补丁内容
git log -p # 显示每次提交的完整差异
# 自定义格式
git log --pretty=format:"%h - %an, %ar : %s"
# 常用格式占位符:
# %h: 短哈希
# %H: 完整哈希
# %an: 作者名
# %ae: 作者邮箱
# %ar: 相对时间
# %s: 提交信息
引用和相对引用
# HEAD 引用
HEAD # 当前分支的最新提交
HEAD~1 或 HEAD^ # 前一个提交
HEAD~2 # 前两个提交
HEAD~n # 前 n 个提交
# 提交哈希
git log --oneline # 查看提交哈希
git show <commit-hash> # 查看指定提交的详细信息
# 分支引用
git log <branch-name> # 查看指定分支的提交历史
git log <branch1>..<branch2> # 查看 branch1 到 branch2 之间的提交
查看提交详情
# 查看指定提交
git show <commit-hash>
git show HEAD # 查看最新提交
git show HEAD~1 # 查看上一个提交
# 查看文件在某个提交时的内容
git show <commit-hash>:<file-path>
远程仓库
查看远程仓库
# 查看远程仓库列表
git remote
# 查看远程仓库详细信息
git remote -v
# 查看远程仓库 URL
git remote get-url origin
# 查看远程分支信息
git remote show origin
添加和删除远程仓库
# 添加远程仓库
git remote add <name> <url>
git remote add origin <url>
# 修改远程仓库 URL
git remote set-url <name> <new-url>
# 删除远程仓库
git remote remove <name>
git remote rm <name> # 旧语法
# 重命名远程仓库
git remote rename <old-name> <new-name>
获取和拉取
# 获取远程更新(不合并)
git fetch <remote-name>
git fetch origin
git fetch --all # 获取所有远程仓库
# 拉取并合并
git pull <remote-name> <branch-name>
git pull # 拉取当前分支的远程更新
git pull origin main
# 拉取并变基
git pull --rebase
git pull --rebase origin main
推送
# 推送到远程
git push <remote-name> <branch-name>
git push origin main
# 推送当前分支
git push
# 首次推送并设置上游
git push -u origin <branch-name>
git push --set-upstream origin <branch-name>
# 强制推送(⚠️ 危险操作)
git push --force # 强制推送(不推荐)
git push --force-with-lease # 更安全的强制推送
# 推送所有分支
git push --all origin
# 推送标签
git push --tags
git push origin <tag-name>
撤销操作
撤销工作区更改
# 撤销工作区单个文件的更改
git checkout -- <file>
git restore <file> # Git 2.23+ 新命令
# 撤销工作区所有更改
git checkout -- .
git restore . # Git 2.23+ 新命令
# 交互式撤销
git checkout -p # 交互式选择要撤销的部分
撤销暂存区更改
# 从暂存区移除文件(保留工作区更改)
git reset HEAD <file>
git restore --staged <file> # Git 2.23+ 新命令
# 从暂存区移除所有文件
git reset HEAD
git restore --staged . # Git 2.23+ 新命令
撤销提交
# 软重置(保留更改在暂存区)
git reset --soft HEAD~1
# 混合重置(保留更改在工作区,默认)
git reset --mixed HEAD~1
git reset HEAD~1 # 同上
# 硬重置(丢弃所有更改,⚠️ 危险)
git reset --hard HEAD~1
# 重置到指定提交
git reset --hard <commit-hash>
# 重置到远程分支状态
git reset --hard origin/<branch-name>
撤销合并
# 撤销未推送的合并
git reset --hard HEAD~1
# 撤销已推送的合并(创建新提交)
git revert -m 1 <merge-commit-hash>
合并与变基
合并(Merge)
# 合并指定分支到当前分支
git merge <branch-name>
# 使用 Fast-forward 合并(如果可以)
git merge --ff <branch-name>
# 禁止 Fast-forward,总是创建合并提交
git merge --no-ff <branch-name>
# 合并时添加提交信息
git merge -m "合并信息" <branch-name>
# 中止合并
git merge --abort
变基(Rebase)
# 将当前分支变基到指定分支
git rebase <branch-name>
git rebase origin/main
# 交互式变基(最近 n 个提交)
git rebase -i HEAD~n
git rebase -i HEAD~3 # 最近 3 个提交
# 交互式变基到指定提交
git rebase -i <commit-hash>
# 继续变基(解决冲突后)
git rebase --continue
# 跳过当前提交
git rebase --skip
# 中止变基
git rebase --abort
交互式变基操作
在交互式变基编辑器中,可以使用以下命令:
pick (p): 使用该提交reword (r): 使用该提交,但修改提交信息edit (e): 使用该提交,但暂停以便修改squash (s): 使用该提交,但合并到上一个提交fixup (f): 类似 squash,但丢弃该提交的日志信息drop (d): 删除该提交
暂存与储藏
储藏(Stash)
# 储藏当前更改
git stash
git stash save "储藏说明"
# 查看储藏列表
git stash list
# 应用最近的储藏(保留储藏)
git stash apply
# 应用指定的储藏
git stash apply stash@{n}
# 应用储藏并删除
git stash pop
git stash pop stash@{n}
# 删除储藏
git stash drop stash@{n}
git stash clear # 删除所有储藏
# 查看储藏内容
git stash show
git stash show -p # 显示详细差异
git stash show stash@{n} -p
标签管理
创建标签
# 创建轻量标签
git tag <tag-name>
git tag v1.0.0
# 创建附注标签
git tag -a <tag-name> -m "标签说明"
git tag -a v1.0.0 -m "版本 1.0.0"
# 在指定提交上打标签
git tag -a <tag-name> <commit-hash> -m "标签说明"
查看标签
# 列出所有标签
git tag
# 搜索标签
git tag -l "v1.*"
# 查看标签信息
git show <tag-name>
推送和删除标签
# 推送标签到远程
git push origin <tag-name>
git push --tags # 推送所有标签
# 删除本地标签
git tag -d <tag-name>
# 删除远程标签
git push origin --delete <tag-name>
git push origin :refs/tags/<tag-name> # 旧语法
查看与对比
查看文件内容
# 查看工作区文件
cat <file>
# 查看暂存区文件
git show :<file>
# 查看指定提交的文件
git show <commit-hash>:<file>
# 查看指定分支的文件
git show <branch-name>:<file>
文件对比
# 工作区 vs 暂存区
git diff <file>
# 暂存区 vs 最新提交
git diff --staged <file>
git diff --cached <file> # 同 --staged
# 两个提交之间的差异
git diff <commit1> <commit2>
# 两个分支之间的差异
git diff <branch1>..<branch2>
git diff <branch1>...<branch2> # 从共同祖先开始的差异
# 统计差异
git diff --stat
搜索
# 在代码中搜索
git grep "关键词"
git grep -n "关键词" # 显示行号
git grep -i "关键词" # 忽略大小写
# 在指定提交中搜索
git grep "关键词" <commit-hash>
配置管理
查看配置
# 查看所有配置
git config --list
# 查看全局配置
git config --global --list
# 查看本地仓库配置
git config --local --list
# 查看特定配置
git config user.name
git config user.email
设置配置
用户信息配置(必配)
# 设置全局用户名和邮箱(推荐,所有仓库使用)
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# 设置本地仓库的用户名和邮箱(仅当前仓库使用)
git config --local user.name "Your Name"
git config --local user.email "your.email@example.com"
# 查看当前使用的用户信息
git config user.name
git config user.email
注意:首次使用 Git 前必须配置用户名和邮箱,否则无法提交代码。如果没有配置,Git 会提示:
*** Please tell me who you are.
其他常用配置
# 设置默认编辑器
git config --global core.editor "code --wait" # VS Code
git config --global core.editor "vim" # Vim
git config --global core.editor "nano" # Nano
# 设置默认分支名
git config --global init.defaultBranch main
# 设置推送方式
git config --global push.default simple
# 设置自动换行处理(Windows)
git config --global core.autocrlf true
# 设置自动换行处理(Linux/macOS)
git config --global core.autocrlf input
常用配置选项
# 颜色输出
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.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual '!code' # 使用 VS Code 查看
# 行尾处理(Windows)
git config --global core.autocrlf true
# 行尾处理(Linux/macOS)
git config --global core.autocrlf input
# 忽略文件大小写(Windows)
git config --global core.ignorecase true
其他常用命令
清理
# 清理未跟踪的文件和目录
git clean -n # 预览要删除的文件
git clean -f # 删除未跟踪的文件
git clean -fd # 删除未跟踪的文件和目录
git clean -fX # 只删除 Git 忽略的文件
git clean -fx # 删除所有未跟踪的文件(包括忽略的)
引用日志(Reflog)
# 查看引用日志
git reflog
git reflog <branch-name>
# 显示最近 n 条
git reflog -n 10
# 恢复丢失的提交
git reflog # 找到提交哈希
git checkout <commit-hash> # 查看或创建新分支
子模块(Submodule)
# 添加子模块
git submodule add <repository-url> <path>
# 初始化子模块
git submodule init
# 更新子模块
git submodule update
git submodule update --init --recursive # 初始化并递归更新
# 克隆包含子模块的仓库
git clone --recursive <repository-url>
文件追踪
# 停止追踪文件(但保留在本地)
git rm --cached <file>
git rm --cached -r <directory> # 递归删除目录
# 移动/重命名文件
git mv <old-name> <new-name>
常用工作流示例
创建功能分支
# 从主分支创建并切换到新分支
git checkout -b feature/new-feature
# 或者使用 switch(Git 2.23+)
git switch -c feature/new-feature
提交更改
# 查看状态
git status
# 添加更改
git add .
# 提交
git commit -m "feat: 添加新功能"
# 推送
git push -u origin feature/new-feature
同步主分支
# 切换到主分支
git checkout main
# 拉取最新更改
git pull
# 切回功能分支
git checkout feature/new-feature
# 变基到主分支
git rebase main
# 或者合并主分支
git merge main
整理提交历史
# 交互式变基最近 3 个提交
git rebase -i HEAD~3
# 在编辑器中:
# - 使用 squash 合并提交
# - 使用 reword 修改提交信息
# - 使用 edit 修改提交内容
注意事项
首次使用必须配置:使用 Git 前必须先配置用户名和邮箱(
git config --global user.name和git config --global user.email),否则无法提交代码强制推送要谨慎:使用
--force-with-lease而不是--force硬重置要小心:
git reset --hard会丢失未提交的更改变基已推送的提交:需要强制推送,可能影响他人
合并冲突:仔细解决,确保代码正确
定期同步:经常从主分支拉取更新,避免冲突积累