Git 常用命令和技巧¶
状态: ✅ 已完成
创建日期: 2026-02-10
最后更新: 2026-02-10
概述¶
本文档整理了日常开发中最常用的 Git 命令和实用技巧,适合作为快速参考手册。
基础配置¶
初次配置¶
# 设置用户信息
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# 查看配置
git config --list
git config user.name
git config user.email
# 设置默认编辑器
git config --global core.editor vim
# 设置默认分支名
git config --global init.defaultBranch main
常用别名配置¶
# 常用命令别名
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'
# 美化 log 输出
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
仓库初始化和克隆¶
# 初始化新仓库
git init
# 克隆远程仓库
git clone <repository-url>
# 克隆指定分支
git clone -b <branch-name> <repository-url>
# 浅克隆(只克隆最近的历史)
git clone --depth 1 <repository-url>
日常操作¶
查看状态¶
# 查看工作区状态
git status
# 简洁输出
git status -s
# 查看分支信息
git branch
git branch -a # 查看所有分支(包括远程)
git branch -v # 查看分支及最后一次提交
添加和提交¶
# 添加文件到暂存区
git add <file>
git add . # 添加所有修改
git add -A # 添加所有修改(包括删除)
git add -p # 交互式添加(部分添加)
# 提交
git commit -m "commit message"
git commit -am "message" # 添加并提交已跟踪文件
git commit --amend # 修改最后一次提交
git commit --amend --no-edit # 修改最后一次提交但不改消息
推送和拉取¶
# 推送到远程
git push
git push origin <branch>
git push -u origin <branch> # 首次推送并设置上游
# 强制推送(危险!)
git push --force
git push --force-with-lease # 更安全的强制推送
# 拉取远程更新
git pull
git pull origin <branch>
git pull --rebase # 使用 rebase 方式拉取
# 仅获取不合并
git fetch
git fetch origin
git fetch --all
分支管理¶
创建和切换分支¶
# 创建分支
git branch <branch-name>
# 切换分支
git checkout <branch-name>
# 创建并切换(推荐)
git checkout -b <branch-name>
# 新语法(Git 2.23+)
git switch <branch-name>
git switch -c <branch-name> # 创建并切换
# 基于远程分支创建本地分支
git checkout -b <local-branch> origin/<remote-branch>
合并分支¶
# 合并分支
git merge <branch-name>
# 不使用 fast-forward
git merge --no-ff <branch-name>
# 查看已合并的分支
git branch --merged
git branch --no-merged
删除分支¶
# 删除本地分支
git branch -d <branch-name>
git branch -D <branch-name> # 强制删除
# 删除远程分支
git push origin --delete <branch-name>
git push origin :<branch-name> # 旧语法
查看历史和差异¶
查看提交历史¶
# 查看提交历史
git log
git log --oneline # 单行显示
git log --graph # 图形化显示
git log -n 5 # 显示最近 5 条
git log --since="2 weeks ago"
git log --author="Author Name"
# 查看文件修改历史
git log <file>
git log -p <file> # 显示每次提交的差异
# 查看某个提交的详细信息
git show <commit-hash>
查看差异¶
# 查看工作区和暂存区的差异
git diff
# 查看暂存区和最后一次提交的差异
git diff --cached
git diff --staged
# 查看两个提交之间的差异
git diff <commit1> <commit2>
# 查看某个文件的差异
git diff <file>
git diff <commit1> <commit2> <file>
查找和搜索¶
# 在提交历史中搜索内容
git log -S "search string" # 搜索添加或删除了某个字符串的提交
git log -G "regex" # 使用正则表达式搜索
# 在工作区搜索
git grep "search string"
# 查找引入 bug 的提交(二分查找)
git bisect start
git bisect bad # 标记当前版本有问题
git bisect good <commit> # 标记某个版本正常
# 然后按提示测试,直到找到问题提交
git bisect reset # 结束查找
撤销操作¶
撤销工作区修改¶
# 撤销工作区的修改(恢复到暂存区状态)
git checkout -- <file>
git restore <file> # 新语法
# 撤销所有工作区修改
git checkout -- .
git restore .
撤销暂存区¶
# 将文件从暂存区移除(保留工作区修改)
git reset HEAD <file>
git restore --staged <file> # 新语法
# 取消所有暂存
git reset HEAD
撤销提交¶
# 撤销最后一次提交(保留修改在工作区)
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 revert <commit-hash>
git revert HEAD # 撤销最后一次提交
恢复删除的文件¶
远程仓库管理¶
# 查看远程仓库
git remote -v
# 添加远程仓库
git remote add origin <url>
# 修改远程仓库地址
git remote set-url origin <new-url>
# 删除远程仓库
git remote remove origin
# 重命名远程仓库
git remote rename origin upstream
# 查看远程分支详细信息
git remote show origin
# 清理远程已删除的分支引用
git remote prune origin
git fetch --prune
暂存工作进度¶
# 暂存当前工作
git stash
git stash save "work in progress"
# 查看暂存列表
git stash list
# 应用暂存
git stash apply # 应用最近的暂存
git stash apply stash@{2} # 应用指定暂存
# 应用并删除暂存
git stash pop
# 删除暂存
git stash drop stash@{0}
git stash clear # 清空所有暂存
# 查看暂存内容
git stash show
git stash show -p # 显示详细差异
标签管理¶
# 创建轻量标签
git tag v1.0.0
# 创建附注标签(推荐)
git tag -a v1.0.0 -m "Release version 1.0.0"
# 为历史提交打标签
git tag -a v0.9.0 <commit-hash>
# 查看标签
git tag
git tag -l "v1.*" # 模式匹配
# 查看标签信息
git show v1.0.0
# 推送标签
git push origin v1.0.0
git push origin --tags # 推送所有标签
# 删除标签
git tag -d v1.0.0 # 删除本地标签
git push origin :refs/tags/v1.0.0 # 删除远程标签
git push origin --delete v1.0.0 # 删除远程标签(新语法)
Rebase 操作¶
# 变基当前分支
git rebase <branch>
# 交互式变基(整理提交历史)
git rebase -i HEAD~3 # 整理最近 3 个提交
git rebase -i <commit-hash>
# 在交互式编辑器中的操作:
# pick = 保留提交
# reword = 保留提交但修改提交消息
# edit = 保留提交但停下来修改
# squash = 合并到前一个提交
# fixup = 合并到前一个提交但丢弃消息
# drop = 删除提交
# 继续 rebase
git rebase --continue
# 跳过当前提交
git rebase --skip
# 中止 rebase
git rebase --abort
子模块管理¶
# 添加子模块
git submodule add <repository-url> <path>
# 初始化子模块
git submodule init
# 更新子模块
git submodule update
# 克隆包含子模块的仓库
git clone --recurse-submodules <repository-url>
# 更新所有子模块到最新
git submodule update --remote
# 删除子模块
git submodule deinit <path>
git rm <path>
实用技巧¶
忽略文件¶
# .gitignore 文件示例
*.log
*.tmp
node_modules/
.env
.DS_Store
# 忽略已跟踪的文件
git rm --cached <file>
git rm -r --cached <directory>
# 查看忽略规则
git check-ignore -v <file>
Cherry-pick(精选提交)¶
# 应用某个提交到当前分支
git cherry-pick <commit-hash>
# 应用多个提交
git cherry-pick <commit1> <commit2>
# 应用提交范围
git cherry-pick <commit1>..<commit2>
清理和优化¶
# 清理未跟踪的文件
git clean -n # 预览要删除的文件
git clean -f # 删除未跟踪的文件
git clean -fd # 删除未跟踪的文件和目录
git clean -fx # 删除未跟踪的文件(包括忽略的文件)
# 优化仓库
git gc # 垃圾回收
git prune # 清理无用对象
# 查看仓库大小
git count-objects -vH
文件重命名和移动¶
查找提交作者¶
# 查看文件每一行的最后修改者
git blame <file>
# 查看指定行范围
git blame -L 10,20 <file>
# 查看文件修改历史(谁改了什么)
git log --follow -p -- <file>
常见问题解决¶
合并冲突¶
# 查看冲突文件
git status
# 手动解决冲突后
git add <resolved-file>
git commit
# 使用工具解决冲突
git mergetool
# 放弃合并
git merge --abort
误提交到错误分支¶
# 方法1: 使用 cherry-pick
git checkout correct-branch
git cherry-pick <commit-hash>
git checkout wrong-branch
git reset --hard HEAD~1
# 方法2: 使用 reset + stash
git reset --soft HEAD~1
git stash
git checkout correct-branch
git stash pop
git commit
找回丢失的提交¶
修改最近的提交消息¶
# 修改最后一次提交消息
git commit --amend -m "New message"
# 修改更早的提交消息
git rebase -i HEAD~3
# 将要修改的提交从 pick 改为 reword
合并多个提交¶
高级技巧¶
临时保存工作切换分支¶
# 不想提交但需要切换分支
git stash
git checkout other-branch
# 工作完成后
git checkout original-branch
git stash pop
只提交部分修改¶
配置不同仓库使用不同的用户信息¶
# 在特定仓库中设置(不使用 --global)
cd /path/to/repo
git config user.name "Work Name"
git config user.email "work@example.com"
使用 Git Hooks¶
# 常用 hooks 位置:.git/hooks/
# pre-commit: 提交前执行
# post-commit: 提交后执行
# pre-push: 推送前执行
# 示例: pre-commit 检查代码风格
#!/bin/sh
npm run lint
查看两个分支的差异¶
# 查看分支差异
git diff branch1..branch2
# 查看一个分支相对另一个分支的所有提交
git log branch1..branch2
# 查看两个分支的分歧点
git merge-base branch1 branch2
最佳实践¶
-
提交消息规范
- 使用清晰、描述性的提交消息
- 第一行简短概述(50字符以内)
- 如需详细说明,空一行后添加正文
- 可使用
feat:,fix:,docs:等前缀
-
分支策略
main/master: 生产环境代码develop: 开发分支feature/*: 功能分支hotfix/*: 紧急修复分支
-
提交频率
- 小而频繁的提交优于大而稀疏的提交
- 每个提交应该是一个逻辑单元
- 提交前确保代码可以运行
-
避免常见错误
- 不要提交敏感信息(密码、密钥)
- 不要提交大文件(使用 Git LFS)
- 不要在公共分支使用
--force - 推送前先拉取最新代码
-
定期维护
- 定期清理已合并的分支
- 定期运行
git gc优化仓库 - 及时解决冲突,不要累积
相关资源¶
快速参考卡片¶
最常用命令¶
git status # 查看状态
git add . # 添加所有修改
git commit -m "msg" # 提交
git push # 推送
git pull # 拉取
git checkout -b branch # 创建并切换分支
git merge branch # 合并分支
git log --oneline # 查看历史