Git 常用命令与命名规范指南
一、基础配置
# 设置用户信息
git config --global user.name "yourname"
git config --global user.email "yourname@example.com"
# 查看配置
git config --list
# 设置默认编辑器
git config --global core.editor vim
# 设置默认分支名
git config --global init.defaultBranch main二、日常操作命令
2.1 仓库初始化与克隆
# 初始化新仓库
git init
# 克隆远程仓库
git clone https://github.com/user/repo.git
# 克隆指定分支、只取最近1次提交(加速大仓库)
git clone --branch develop --depth 1 https://github.com/user/repo.git2.2 文件暂存与提交
# 查看状态
git status
# 添加文件到暂存区
git add file.txt # 单个文件
git add src/ # 整个目录
git add -A # 所有变更(慎用)
# 提交
git commit -m "feat: add login page"
# 追加到上一次提交(未 push 时使用)
git commit --amend -m "feat: add login page with validation"
# 查看暂存区与工作区的差异
git diff # 工作区 vs 暂存区
git diff --staged # 暂存区 vs 最新提交2.3 查看历史
# 查看提交日志
git log --oneline --graph --all # 精简图形化
git log -5 # 最近5条
git log --author="yourname" # 按作者过滤
git log --since="2026-01-01" # 按时间过滤
git log -- path/to/file # 某文件的历史
# 查看某次提交的内容
git show <commit-sha>
# 查看某文件每行的最后修改者
git blame file.txt2.4 比较差异
# 比较两个提交
git diff <commit1> <commit2>
# 只看文件列表
git diff --name-only <commit1> <commit2>
# 统计增删行数
git diff --stat <commit1> <commit2>
# 比较两个分支
git diff main..feature/login
# 比较某个文件在两个版本间的变化
git diff HEAD~3 HEAD -- src/login.py2.5 撤销与回退
# 撤销工作区修改(未 add)
git checkout -- file.txt # 旧写法
git restore file.txt # 新写法(推荐)
# 撤销暂存(已 add,未 commit)
git reset HEAD file.txt # 旧写法
git restore --staged file.txt # 新写法(推荐)
# 回退到某个提交(保留修改在工作区)
git reset --soft HEAD~1
# 回退到某个提交(丢弃修改,慎用)
git reset --hard HEAD~1
# 生成一个新提交来撤销某次提交(安全,适合已 push 的)
git revert <commit-sha>2.6 暂存工作区 (stash)
# 场景:正在开发功能,突然需要切分支修 bug
git stash # 暂存当前修改
git stash save "WIP: login" # 带说明
git stash list # 查看暂存列表
git stash pop # 恢复最近一次并删除记录
git stash apply stash@{1} # 恢复指定记录(不删除)
git stash drop stash@{0} # 删除某条记录三、分支操作
3.1 基本分支命令
# 查看分支
git branch # 本地分支
git branch -r # 远程分支
git branch -a # 所有分支
# 创建分支
git branch feature/login
# 切换分支
git checkout feature/login # 旧写法
git switch feature/login # 新写法(推荐)
# 创建并切换
git checkout -b feature/login
git switch -c feature/login
# 删除分支
git branch -d feature/login # 已合并才能删
git branch -D feature/login # 强制删除
# 重命名分支
git branch -m old-name new-name3.2 合并与变基
# 合并(保留分支历史)
git checkout main
git merge feature/login
# 变基(线性历史,适合个人分支)
git checkout feature/login
git rebase main
# 交互式变基(整理提交历史)
git rebase -i HEAD~3 # 合并/修改最近3个提交
# 中止合并或变基
git merge --abort
git rebase --abort3.3 Cherry-pick
# 场景:只想把某个提交应用到当前分支
git cherry-pick <commit-sha>
# 批量 cherry-pick
git cherry-pick <sha1> <sha2> <sha3>
# 只暂存不提交
git cherry-pick --no-commit <commit-sha>四、远程操作
# 查看远程仓库
git remote -v
# 添加远程仓库
git remote add upstream https://github.com/original/repo.git
# 拉取更新
git fetch origin # 只获取,不合并
git pull origin main # 获取并合并(= fetch + merge)
git pull --rebase origin main # 获取并变基(更干净的历史)
# 推送
git push origin feature/login
git push -u origin feature/login # 首次推送并设置追踪
# 删除远程分支
git push origin --delete feature/login
# 强制推送(慎用,仅限个人分支)
git push --force-with-lease origin feature/login五、标签管理
# 创建标签
git tag v1.0.0 # 轻量标签
git tag -a v1.0.0 -m "Release 1.0.0" # 带注释标签
# 查看标签
git tag -l "v1.*"
# 推送标签到远程
git push origin v1.0.0
git push origin --tags # 推送所有标签
# 删除标签
git tag -d v1.0.0
git push origin --delete v1.0.0六、命名规范
6.1 分支命名
| 类型 | 格式 | 示例 | 场景 |
|---|---|---|---|
| 功能分支 | feature/<描述> | feature/user-login | 新功能开发 |
| 修复分支 | fix/<描述> | fix/crash-on-launch | Bug 修复 |
| 热修复 | hotfix/<描述> | hotfix/security-patch | 线上紧急修复 |
| 发布分支 | release/<版本> | release/v2.1.0 | 版本发布准备 |
| 重构分支 | refactor/<描述> | refactor/auth-module | 代码重构 |
| 实验分支 | experiment/<描述> | experiment/new-algorithm | 技术验证 |
规则:
- 全部小写,用
-连接单词 - 简洁明确,体现改动意图
- 可加 JIRA/Issue 编号:
feature/PROJ-123-user-login
6.2 Commit Message 规范(Conventional Commits)
格式:
<type>(<scope>): <subject>
<body>
<footer>
Type 类型:
| Type | 含义 | 示例 |
|---|---|---|
feat | 新功能 | feat(auth): add OAuth2 login |
fix | Bug 修复 | fix(ui): resolve crash on rotation |
docs | 文档变更 | docs: update API reference |
style | 格式调整(不影响逻辑) | style: fix indentation |
refactor | 重构(不改功能) | refactor(db): simplify query logic |
perf | 性能优化 | perf: reduce memory allocation in parser |
test | 测试相关 | test: add unit tests for login |
chore | 构建/工具变更 | chore: upgrade gradle to 8.2 |
ci | CI/CD 变更 | ci: add lint check to pipeline |
revert | 回退提交 | revert: revert "feat(auth): add OAuth2" |
示例:
# 简单提交
git commit -m "feat: add user avatar upload"
# 带 scope
git commit -m "fix(video): resolve playback stutter on low-end devices"
# 带 body 说明
git commit -m "refactor(network): replace OkHttp with Ktor
Ktor provides better coroutine support and reduces boilerplate.
Migration involves updating all API service interfaces."
# Breaking change
git commit -m "feat(api)!: change response format to v2
BREAKING CHANGE: /users endpoint now returns paginated results.
Clients must handle the new pagination fields."6.3 Tag 命名
v<MAJOR>.<MINOR>.<PATCH>[-<pre-release>]
| 示例 | 场景 |
|---|---|
v1.0.0 | 首个正式版本 |
v1.1.0 | 向后兼容的功能新增 |
v1.1.1 | 向后兼容的 bug 修复 |
v2.0.0 | 不兼容的 API 变更 |
v2.0.0-beta.1 | 预发布版本 |
v2.0.0-rc.1 | Release Candidate |
七、实用场景速查
场景 1:开发新功能的完整流程
# 1. 从 main 创建功能分支
git switch main
git pull origin main
git switch -c feature/image-compress
# 2. 开发过程中多次小提交
git add src/compress.py
git commit -m "feat(image): add JPEG compression utility"
git add src/compress_test.py
git commit -m "test(image): add compression ratio tests"
# 3. 开发完成,整理提交(可选)
git rebase -i main # squash 多余的 WIP 提交
# 4. 推送并创建 MR/PR
git push -u origin feature/image-compress场景 2:修复线上紧急 bug
# 1. 从 main 拉 hotfix 分支
git switch main
git pull origin main
git switch -c hotfix/null-pointer-crash
# 2. 修复并提交
git add src/app.py
git commit -m "fix: handle null user in profile page
Crash occurred when user data was not yet loaded.
Added null check before accessing user.name."
# 3. 推送、合并、打 tag
git push -u origin hotfix/null-pointer-crash
# 合并后
git tag -a v1.2.1 -m "Hotfix: null pointer crash"
git push origin v1.2.1场景 3:同步上游仓库(Fork 工作流)
# 1. 添加上游
git remote add upstream https://github.com/original/repo.git
# 2. 拉取上游最新
git fetch upstream
# 3. 合并到本地 main
git switch main
git merge upstream/main
# 4. 推送到自己的 fork
git push origin main场景 4:找回误删的提交
# 查看所有操作记录(包括被 reset 掉的)
git reflog
# 找到目标 SHA 后恢复
git checkout -b recovery <commit-sha>场景 5:大文件处理
# 安装 Git LFS
git lfs install
# 追踪大文件类型
git lfs track "*.psd"
git lfs track "*.so"
# 确保 .gitattributes 被提交
git add .gitattributes
git commit -m "chore: track large files with LFS"八、.gitignore 常用模板
# IDE
.idea/
.vscode/
*.iml
# Build
build/
out/
*.apk
*.aab
# Python
__pycache__/
*.pyc
.venv/
# OS
.DS_Store
Thumbs.db
# Secrets
.env
*.key
credentials.json九、Git Alias 推荐配置
git config --global alias.st "status"
git config --global alias.co "checkout"
git config --global alias.sw "switch"
git config --global alias.br "branch"
git config --global alias.ci "commit"
git config --global alias.lg "log --oneline --graph --all --decorate"
git config --global alias.last "log -1 HEAD"
git config --global alias.unstage "restore --staged"
git config --global alias.amend "commit --amend --no-edit"使用:
git st # = git status
git lg # 图形化日志
git unstage file.txt # 取消暂存