Git branch management, conflict resolution, commit conventions, improving team collaboration efficiency.
Core Features
| Feature | Description |
|---|---|
| Branch Management | Create, switch, and merge branches |
| Conflict Resolution | Identify and resolve conflicts |
| Commit Conventions | Conventional Commits |
| Common Issues | Undo, rollback, restore |
How to Use
Branch Operations
创建并切换到新分支 feature/login
Conflict Resolution
帮我解决 Git 合并冲突
Commit Suggestions
为这些改动生成符合规范的提交信息
Common Commands Quick Reference
Basic Operations
# 初始化
git init # 初始化仓库
git clone URL # 克隆仓库
# 配置
git config --global user.name "Your Name"
git config --global user.email "email@example.com"
# 状态查看
git status # 查看状态
git log --oneline # 简洁日志
git log --graph --oneline --all # 图形化日志
git diff # 查看改动
git show COMMIT_ID # 查看提交详情
Branch Management
# 查看分支
git branch # 本地分支
git branch -a # 所有分支
git branch -r # 远程分支
# 创建分支
git branch feature/login # 创建分支
git checkout -b feature/login # 创建并切换
git switch -c feature/login # 新语法
# 切换分支
git checkout feature/login # 切换分支
git switch feature/login # 新语法
# 合并分支
git merge feature/login # 合并分支
git merge --no-ff feature/login # 禁用快进
# 删除分支
git branch -d feature/login # 安全删除
git branch -D feature/login # 强制删除
git push origin --delete feature/login # 删除远程分支
Remote Operations
# 查看远程
git remote -v # 查看远程仓库
git remote add origin URL # 添加远程
git remote set-url origin URL # 修改远程地址
# 拉取推送
git fetch origin # 获取更新
git pull origin main # 拉取并合并
git push origin main # 推送
git push -u origin feature/login # 推送并设置上游
# 同步远程
git remote prune origin # 清理无效引用
git fetch --all --prune # 获取并清理
Stash Operations
# 暂存工作区
git stash # 暂存当前修改
git stash save "message" # 带消息暂存
git stash list # 查看暂存列表
git stash pop # 恢复并删除
git stash apply # 恢复但不删除
git stash drop stash@{0} # 删除指定暂存
git stash clear # 清空暂存
Commit Conventions
Conventional Commits
<type>(<scope>): <subject>
<body>
<footer>
Type
| Type | Description | Example |
|---|---|---|
| feat | New feature | feat(auth): add login feature |
| fix | Fix bug | fix(api): fix API timeout |
| docs | Documentation update | docs(readme): update installation instructions |
| style | Code formatting | style: format code |
| refactor | Refactor | refactor(utils): optimize utility functions |
| perf | Performance optimization | perf(list): optimize list rendering |
| test | Testing | test(auth): add login tests |
| chore | Build/tools | chore: update dependencies |
| ci | CI configuration | ci: add GitHub Actions |
| revert | Revert | revert: revert login feature |
Commit Examples
# 功能
git commit -m "feat(user): 添加用户注册功能"
# 修复
git commit -m "fix(payment): 修复支付金额计算错误"
# 文档
git commit -m "docs(api): 更新接口文档"
# 重构
git commit -m "refactor(auth): 重构认证逻辑
- 提取公共方法
- 优化错误处理
- 添加单元测试"
# 破坏性变更
git commit -m "feat(api)!: 修改 API 响应格式
BREAKING CHANGE: 响应格式从 {code, data} 改为 {status, result}"
Workflow Patterns
Git Flow
# 初始化
git flow init
# 功能开发
git flow feature start login
git flow feature finish login
# 发布
git flow release start v1.0.0
git flow release finish v1.0.0
# 热修复
git flow hotfix start fix-login-bug
git flow hotfix finish fix-login-bug
GitHub Flow
# 1. 从 main 创建分支
git checkout -b feature/login main
# 2. 开发并提交
git add .
git commit -m "feat: 添加登录功能"
git push origin feature/login
# 3. 创建 Pull Request
# 4. Code Review
# 5. 合并到 main
git checkout main
git pull origin main
git merge feature/login
git push origin main
GitLab Flow
# 环境分支:main -> staging -> production
# 1. 功能开发
git checkout -b feature/login main
git push origin feature/login
# 2. 合并到 main
git checkout main
git merge feature/login
# 3. 部署到 staging
git checkout staging
git merge main
git push origin staging
# 4. 部署到 production
git checkout production
git merge staging
git push origin production
Conflict Resolution
Identify Conflicts
# 查看冲突文件
git status
# 冲突标记
<<<<<<< HEAD
当前分支内容
=======
合并分支内容
>>>>>>> feature/login
Resolve Conflicts
# 1. 手动编辑冲突文件
# 2. 选择保留的内容
# 3. 删除冲突标记
# 4. 添加解决后的文件
git add .
# 5. 完成合并
git commit
Conflict Tools
# 使用合并工具
git mergetool
# 使用 VS Code
git config --global merge.tool code
git config --global mergetool.code.cmd 'code --wait $MERGED'
# 查看差异
git diff HEAD # 与 HEAD 比较
git diff --ours # 使用当前版本
git diff --theirs # 使用合并版本
Common Problem Resolution
Undo Operations
# 撤销工作区修改
git checkout -- file.txt
git restore file.txt # 新语法
# 撤销暂存
git reset HEAD file.txt
git restore --staged file.txt # 新语法
# 撤销最近提交(保留修改)
git reset --soft HEAD~1
# 撤销最近提交(丢弃修改)
git reset --hard HEAD~1
# 修改最近提交信息
git commit --amend -m "新信息"
Rollback Code
# 回滚指定提交
git revert COMMIT_ID
# 回滚多个提交
git revert COMMIT_ID1..COMMIT_ID2
# 回滚但不提交
git revert --no-commit COMMIT_ID
Restore Deleted
# 恢复删除的文件
git checkout COMMIT_ID -- file.txt
# 恢复删除的提交
git reflog # 查找提交
git reset --hard COMMIT_ID # 恢复
# 恢复删除的分支
git reflog # 找到分支最后的提交
git checkout -b branch_name COMMIT_ID
Clean Up Repository
# 清理未跟踪文件
git clean -n # 预览
git clean -f # 删除文件
git clean -fd # 删除文件和目录
# 清理历史
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch path/to/file' \
--prune-empty --tag-name-filter cat -- --all
# 使用 BFG(更快)
bfg --delete-folders folder_name
git reflog expire --expire=now --all
git gc --prune=now --aggressive
Alias Configuration
# 添加到 ~/.gitconfig
[alias]
co = checkout
br = branch
ci = commit
st = status
unstage = reset HEAD --
last = log -1 HEAD
visual = log --graph --oneline --all
amend = commit --amend --no-edit
undo = reset --soft HEAD~1
wip = !git add -A && git commit -m 'WIP'
Notes
- Pull the latest code before committing
- Merge feature branches promptly
- Avoid committing directly to main
- Back up before important operations
Created: 2026-03-12 Version: 1.0