first commit
This commit is contained in:
477
文档润色流和知识库构建流/claude-scholar/skills/git-workflow/SKILL.md
Normal file
477
文档润色流和知识库构建流/claude-scholar/skills/git-workflow/SKILL.md
Normal file
@@ -0,0 +1,477 @@
|
||||
---
|
||||
name: git-workflow
|
||||
description: This skill should be used when the user asks to "create git commit", "manage branches", "follow git workflow", "use Conventional Commits", "handle merge conflicts", or asks about git branching strategies, version control best practices, pull request workflows. Provides comprehensive Git workflow guidance for team collaboration.
|
||||
version: 1.2.0
|
||||
---
|
||||
|
||||
# Git Workflow Standards
|
||||
|
||||
This document defines the project's Git usage standards, including commit message format, branch management strategy, workflows, merge strategies, and more. Following these standards improves collaboration efficiency, enables traceability, supports automation, and reduces conflicts.
|
||||
|
||||
## Commit Message Standards
|
||||
|
||||
The project follows the **Conventional Commits** specification:
|
||||
|
||||
```
|
||||
<type>(<scope>): <subject>
|
||||
|
||||
<body>
|
||||
|
||||
<footer>
|
||||
```
|
||||
|
||||
### Type Reference
|
||||
|
||||
| Type | Description | Example |
|
||||
| :--- | :--- | :--- |
|
||||
| `feat` | New feature | `feat(user): add user export functionality` |
|
||||
| `fix` | Bug fix | `fix(login): fix captcha not refreshing` |
|
||||
| `docs` | Documentation update | `docs(api): update API documentation` |
|
||||
| `refactor` | Refactoring | `refactor(utils): refactor utility functions` |
|
||||
| `perf` | Performance improvement | `perf(list): optimize list performance` |
|
||||
| `test` | Test related | `test(user): add unit tests` |
|
||||
| `chore` | Other changes | `chore: update dependency versions` |
|
||||
|
||||
### Subject Rules
|
||||
|
||||
- Start with a verb: add, fix, update, remove, optimize
|
||||
- No more than 50 characters
|
||||
- No period at the end
|
||||
|
||||
For more detailed conventions and examples, see `references/commit-conventions.md`.
|
||||
|
||||
## Branch Management Strategy
|
||||
|
||||
### Branch Types
|
||||
|
||||
| Branch Type | Naming Convention | Description | Lifecycle |
|
||||
| :--- | :--- | :--- | :--- |
|
||||
| master | `master` | Main branch, releasable state | Permanent |
|
||||
| develop | `develop` | Development branch, latest integrated code | Permanent |
|
||||
| feature | `feature/feature-name` | Feature branch | Delete after completion |
|
||||
| bugfix | `bugfix/issue-description` | Bug fix branch | Delete after fix |
|
||||
| hotfix | `hotfix/issue-description` | Emergency fix branch | Delete after fix |
|
||||
| release | `release/version-number` | Release branch | Delete after release |
|
||||
|
||||
### Branch Naming Examples
|
||||
|
||||
```
|
||||
feature/user-management # User management feature
|
||||
feature/123-add-export # Issue-linked feature
|
||||
bugfix/login-error # Login error fix
|
||||
hotfix/security-vulnerability # Security vulnerability fix
|
||||
release/v1.0.0 # Version release
|
||||
```
|
||||
|
||||
### Branch Protection Rules
|
||||
|
||||
**master branch:**
|
||||
- No direct pushes allowed
|
||||
- Must merge via Pull Request
|
||||
- Must pass CI checks
|
||||
- Requires at least one Code Review approval
|
||||
|
||||
**develop branch:**
|
||||
- Direct pushes restricted
|
||||
- Pull Request merges recommended
|
||||
- Must pass CI checks
|
||||
|
||||
For detailed branch strategies and workflows, see `references/branching-strategies.md`.
|
||||
|
||||
## Workflows
|
||||
|
||||
### Daily Development Workflow
|
||||
|
||||
```bash
|
||||
# 1. Sync latest code
|
||||
git checkout develop
|
||||
git pull origin develop
|
||||
|
||||
# 2. Create feature branch
|
||||
git checkout -b feature/user-management
|
||||
|
||||
# 3. Develop and commit
|
||||
git add .
|
||||
git commit -m "feat(user): add user list page"
|
||||
|
||||
# 4. Push to remote
|
||||
git push -u origin feature/user-management
|
||||
|
||||
# 5. Create Pull Request and request Code Review
|
||||
|
||||
# 6. Merge to develop (via PR)
|
||||
|
||||
# 7. Delete feature branch
|
||||
git branch -d feature/user-management
|
||||
git push origin -d feature/user-management
|
||||
```
|
||||
|
||||
### Hotfix Workflow
|
||||
|
||||
```bash
|
||||
# 1. Create fix branch from master
|
||||
git checkout master
|
||||
git pull origin master
|
||||
git checkout -b hotfix/critical-bug
|
||||
|
||||
# 2. Fix and commit
|
||||
git add .
|
||||
git commit -m "fix(auth): fix authentication bypass vulnerability"
|
||||
|
||||
# 3. Merge to master
|
||||
git checkout master
|
||||
git merge --no-ff hotfix/critical-bug
|
||||
git tag -a v1.0.1 -m "hotfix: fix authentication bypass vulnerability"
|
||||
git push origin master --tags
|
||||
|
||||
# 4. Sync to develop
|
||||
git checkout develop
|
||||
git merge --no-ff hotfix/critical-bug
|
||||
git push origin develop
|
||||
```
|
||||
|
||||
### Release Workflow
|
||||
|
||||
```bash
|
||||
# 1. Create release branch
|
||||
git checkout develop
|
||||
git checkout -b release/v1.0.0
|
||||
|
||||
# 2. Update version numbers and documentation
|
||||
|
||||
# 3. Commit version update
|
||||
git add .
|
||||
git commit -m "chore(release): prepare release v1.0.0"
|
||||
|
||||
# 4. Merge to master
|
||||
git checkout master
|
||||
git merge --no-ff release/v1.0.0
|
||||
git tag -a v1.0.0 -m "release: v1.0.0 official release"
|
||||
git push origin master --tags
|
||||
|
||||
# 5. Sync to develop
|
||||
git checkout develop
|
||||
git merge --no-ff release/v1.0.0
|
||||
git push origin develop
|
||||
```
|
||||
|
||||
## Merge Strategy
|
||||
|
||||
### Merge vs Rebase
|
||||
|
||||
| Feature | Merge | Rebase |
|
||||
| :--- | :--- | :--- |
|
||||
| History | Preserves complete history | Linear history |
|
||||
| Use case | Public branches | Private branches |
|
||||
| Recommended for | Merging to main branch | Syncing upstream code |
|
||||
|
||||
### Recommendations
|
||||
|
||||
- **Feature branch syncing develop**: Use `rebase`
|
||||
- **Feature branch merging to develop**: Use `merge --no-ff`
|
||||
- **develop merging to master**: Use `merge --no-ff`
|
||||
|
||||
```bash
|
||||
# ✅ Recommended: Feature branch syncing develop
|
||||
git checkout feature/user-management
|
||||
git rebase develop
|
||||
|
||||
# ✅ Recommended: Merge feature branch to develop
|
||||
git checkout develop
|
||||
git merge --no-ff feature/user-management
|
||||
|
||||
# ❌ Not recommended: Rebase on public branch
|
||||
git checkout develop
|
||||
git rebase feature/xxx # Dangerous operation
|
||||
```
|
||||
|
||||
**Project convention**: Use `--no-ff` when merging feature branches to preserve branch history.
|
||||
|
||||
For detailed merge strategies and techniques, see `references/merge-strategies.md`.
|
||||
|
||||
## Conflict Resolution
|
||||
|
||||
### Identifying Conflicts
|
||||
|
||||
```
|
||||
<<<<<<< HEAD
|
||||
// Current branch code
|
||||
const name = 'Alice'
|
||||
=======
|
||||
// Branch being merged
|
||||
const name = 'Bob'
|
||||
>>>>>>> feature/user-management
|
||||
```
|
||||
|
||||
### Resolving Conflicts
|
||||
|
||||
```bash
|
||||
# 1. View conflicting files
|
||||
git status
|
||||
|
||||
# 2. Manually edit files to resolve conflicts
|
||||
|
||||
# 3. Mark as resolved
|
||||
git add <file>
|
||||
|
||||
# 4. Complete the merge
|
||||
git commit # merge conflict
|
||||
# or
|
||||
git rebase --continue # rebase conflict
|
||||
```
|
||||
|
||||
### Conflict Resolution Strategies
|
||||
|
||||
```bash
|
||||
# Keep current branch version
|
||||
git checkout --ours <file>
|
||||
|
||||
# Keep incoming branch version
|
||||
git checkout --theirs <file>
|
||||
|
||||
# Abort merge
|
||||
git merge --abort
|
||||
git rebase --abort
|
||||
```
|
||||
|
||||
### Preventing Conflicts
|
||||
|
||||
1. **Sync code regularly** - Pull latest code before starting work each day
|
||||
2. **Small commits** - Commit small changes frequently
|
||||
3. **Modular features** - Implement different features in different files
|
||||
4. **Communication** - Avoid modifying the same file simultaneously
|
||||
|
||||
For detailed conflict handling and advanced techniques, see `references/conflict-resolution.md`.
|
||||
|
||||
## .gitignore Standards
|
||||
|
||||
### Basic Rules
|
||||
|
||||
```
|
||||
# Ignore all .log files
|
||||
*.log
|
||||
|
||||
# Ignore directories
|
||||
node_modules/
|
||||
|
||||
# Ignore directory at root
|
||||
/temp/
|
||||
|
||||
# Ignore files in all directories
|
||||
**/.env
|
||||
|
||||
# Don't ignore specific files
|
||||
!.gitkeep
|
||||
```
|
||||
|
||||
### Common .gitignore
|
||||
|
||||
```
|
||||
node_modules/
|
||||
dist/
|
||||
build/
|
||||
.idea/
|
||||
.vscode/
|
||||
.env
|
||||
.env.local
|
||||
logs/
|
||||
*.log
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
```
|
||||
|
||||
For detailed .gitignore patterns and project-specific configurations, see `references/gitignore-guide.md`.
|
||||
|
||||
## Tag Management
|
||||
|
||||
Uses **Semantic Versioning**:
|
||||
|
||||
```
|
||||
MAJOR.MINOR.PATCH[-PRERELEASE]
|
||||
```
|
||||
|
||||
### Version Change Rules
|
||||
|
||||
- **MAJOR**: Incompatible API changes (v1.0.0 → v2.0.0)
|
||||
- **MINOR**: Backward-compatible new features (v1.0.0 → v1.1.0)
|
||||
- **PATCH**: Backward-compatible bug fixes (v1.0.0 → v1.0.1)
|
||||
|
||||
### Tag Operations
|
||||
|
||||
```bash
|
||||
# Create annotated tag (recommended)
|
||||
git tag -a v1.0.0 -m "release: v1.0.0 official release"
|
||||
|
||||
# Push tags
|
||||
git push origin v1.0.0
|
||||
git push origin --tags
|
||||
|
||||
# View tags
|
||||
git tag
|
||||
git show v1.0.0
|
||||
|
||||
# Delete tag
|
||||
git tag -d v1.0.0
|
||||
git push origin :refs/tags/v1.0.0
|
||||
```
|
||||
|
||||
## Team Collaboration Standards
|
||||
|
||||
### Pull Request Standards
|
||||
|
||||
PRs should include:
|
||||
|
||||
```markdown
|
||||
## Change Description
|
||||
<!-- Describe the content and purpose of this change -->
|
||||
|
||||
## Change Type
|
||||
- [ ] New feature (feat)
|
||||
- [ ] Bug fix (fix)
|
||||
- [ ] Code refactoring (refactor)
|
||||
|
||||
## Testing Method
|
||||
<!-- Describe how to test -->
|
||||
|
||||
## Related Issue
|
||||
Closes #xxx
|
||||
|
||||
## Checklist
|
||||
- [ ] Code has been self-tested
|
||||
- [ ] Documentation has been updated
|
||||
```
|
||||
|
||||
### Code Review Standards
|
||||
|
||||
Review focus areas:
|
||||
- **Code quality**: Clear and readable, proper naming, no duplicate code
|
||||
- **Logic correctness**: Business logic correct, edge cases handled
|
||||
- **Security**: No security vulnerabilities, sensitive information protected
|
||||
- **Performance**: No obvious performance issues, resources properly released
|
||||
|
||||
For detailed collaboration standards and best practices, see `references/collaboration.md`.
|
||||
|
||||
## Common Issues
|
||||
|
||||
### Amending the Last Commit
|
||||
|
||||
```bash
|
||||
# Amend commit content (not yet pushed)
|
||||
git add forgotten-file.ts
|
||||
git commit --amend --no-edit
|
||||
|
||||
# Amend commit message
|
||||
git commit --amend -m "new commit message"
|
||||
```
|
||||
|
||||
### Push Rejected
|
||||
|
||||
```bash
|
||||
# Pull then push
|
||||
git pull origin master
|
||||
git push origin master
|
||||
|
||||
# Use rebase for cleaner history
|
||||
git pull --rebase origin master
|
||||
git push origin master
|
||||
```
|
||||
|
||||
### Rollback to Previous Version
|
||||
|
||||
```bash
|
||||
# Reset to specific commit (discards subsequent commits)
|
||||
git reset --hard abc123
|
||||
|
||||
# Create reverse commit (recommended, preserves history)
|
||||
git revert abc123
|
||||
```
|
||||
|
||||
### Stash Current Work
|
||||
|
||||
```bash
|
||||
git stash save "work in progress"
|
||||
git stash list
|
||||
git stash pop
|
||||
```
|
||||
|
||||
### View File Modification History
|
||||
|
||||
```bash
|
||||
git log -- <file> # Commit history
|
||||
git log -p -- <file> # Detailed content
|
||||
git blame <file> # Per-line author
|
||||
```
|
||||
|
||||
## Best Practices Summary
|
||||
|
||||
### Commit Standards
|
||||
|
||||
✅ **Recommended**:
|
||||
- Follow Conventional Commits specification
|
||||
- Write clear commit messages describing changes
|
||||
- One commit for one logical change
|
||||
- Run code checks before committing
|
||||
|
||||
❌ **Prohibited**:
|
||||
- Vague commit messages
|
||||
- Multiple unrelated changes in one commit
|
||||
- Committing sensitive information (passwords, keys)
|
||||
- Developing directly on main branch
|
||||
|
||||
### Branch Management
|
||||
|
||||
✅ **Recommended**:
|
||||
- Use feature branches for development
|
||||
- Regularly sync main branch code
|
||||
- Delete branches promptly after feature completion
|
||||
- Use `--no-ff` merge to preserve history
|
||||
|
||||
❌ **Prohibited**:
|
||||
- Developing directly on main branch
|
||||
- Long-lived unmerged feature branches
|
||||
- Non-standard branch naming
|
||||
- Rebasing on public branches
|
||||
|
||||
### Code Review
|
||||
|
||||
✅ **Recommended**:
|
||||
- All code goes through Pull Requests
|
||||
- At least one reviewer approval before merging
|
||||
- Provide constructive feedback
|
||||
|
||||
❌ **Prohibited**:
|
||||
- Merging without review
|
||||
- Reviewing your own code
|
||||
|
||||
## Additional Resources
|
||||
|
||||
### Reference Files
|
||||
|
||||
For detailed guidance on specific topics:
|
||||
|
||||
- **`references/commit-conventions.md`** - Commit message detailed conventions and examples
|
||||
- **`references/branching-strategies.md`** - Comprehensive branch management strategies
|
||||
- **`references/merge-strategies.md`** - Merge, rebase, and conflict resolution strategies
|
||||
- **`references/conflict-resolution.md`** - Detailed conflict handling and prevention
|
||||
- **`references/advanced-usage.md`** - Git performance optimization, security, submodules, and advanced techniques
|
||||
- **`references/collaboration.md`** - Pull request and code review guidelines
|
||||
- **`references/gitignore-guide.md`** - .gitignore patterns and project-specific configurations
|
||||
|
||||
### Example Files
|
||||
|
||||
Working examples in `examples/`:
|
||||
- **`examples/commit-messages.txt`** - Good commit message examples
|
||||
- **`examples/workflow-commands.sh`** - Common workflow command snippets
|
||||
|
||||
## Summary
|
||||
|
||||
This document defines the project's Git standards:
|
||||
|
||||
1. **Commit Messages** - Follow Conventional Commits specification
|
||||
2. **Branch Management** - master/develop/feature/bugfix/hotfix/release branch strategy
|
||||
3. **Workflows** - Standard processes for daily development, hotfixes, and releases
|
||||
4. **Merge Strategy** - Use rebase to sync feature branches, merge --no-ff to merge
|
||||
5. **Tag Management** - Semantic versioning, annotated tags
|
||||
6. **Conflict Resolution** - Regular syncing, small commits, team communication
|
||||
|
||||
Following these standards improves collaboration efficiency, ensures code quality, and simplifies version management.
|
||||
@@ -0,0 +1,50 @@
|
||||
# 好的 Commit Message 示例
|
||||
|
||||
## 简单提交
|
||||
|
||||
feat(user): 添加用户导出功能
|
||||
|
||||
fix(login): 修复验证码不刷新问题
|
||||
|
||||
docs(readme): 更新安装说明
|
||||
|
||||
## 带 Body 的提交
|
||||
|
||||
feat(user): 添加用户批量导入功能
|
||||
|
||||
- 支持 Excel 文件导入
|
||||
- 支持数据校验和错误提示
|
||||
- 支持导入进度显示
|
||||
|
||||
相关需求: #123
|
||||
|
||||
fix(login): 修复验证码不刷新问题
|
||||
|
||||
原因: 缓存时间设置过长导致验证码一直显示同一张图片
|
||||
方案: 将缓存时间从5分钟调整为1分钟
|
||||
|
||||
## 带 Footer 的提交
|
||||
|
||||
feat(api): 重构用户接口
|
||||
|
||||
BREAKING CHANGE: 用户查询接口路径变更
|
||||
旧路径: /api/user/list
|
||||
新路径: /api/system/user/list
|
||||
|
||||
Closes #789
|
||||
|
||||
## 特殊类型提交
|
||||
|
||||
revert: 回滚 feat(user)
|
||||
|
||||
回滚提交 abc123,因为引入了性能问题
|
||||
|
||||
chore(release): 准备发布 v1.0.0
|
||||
|
||||
- 更新版本号到 1.0.0
|
||||
- 更新 CHANGELOG.md
|
||||
- 更新 README.md
|
||||
|
||||
test(user): 添加用户模块单元测试
|
||||
|
||||
覆盖用户注册、登录、资料修改功能
|
||||
@@ -0,0 +1,132 @@
|
||||
#!/bin/bash
|
||||
# Git 工作流常用命令示例
|
||||
|
||||
# ============================================
|
||||
# 日常开发流程
|
||||
# ============================================
|
||||
|
||||
# 1. 同步最新代码
|
||||
git checkout develop
|
||||
git pull origin develop
|
||||
|
||||
# 2. 创建功能分支
|
||||
git checkout -b feature/user-management
|
||||
|
||||
# 3. 开发并提交
|
||||
git add .
|
||||
git commit -m "feat(user): 添加用户列表页面"
|
||||
|
||||
# 4. 推送到远程
|
||||
git push -u origin feature/user-management
|
||||
|
||||
# 5. 同步上游更新到功能分支
|
||||
git fetch origin develop
|
||||
git rebase origin/develop
|
||||
|
||||
# 6. 合并到 develop(通过 PR 或直接)
|
||||
git checkout develop
|
||||
git merge --no-ff feature/user-management
|
||||
git push origin develop
|
||||
|
||||
# 7. 删除功能分支
|
||||
git branch -d feature/user-management
|
||||
git push origin -d feature/user-management
|
||||
|
||||
# ============================================
|
||||
# 紧急修复流程
|
||||
# ============================================
|
||||
|
||||
# 1. 从 master 创建修复分支
|
||||
git checkout master
|
||||
git pull origin master
|
||||
git checkout -b hotfix/security-fix
|
||||
|
||||
# 2. 修复并提交
|
||||
git add .
|
||||
git commit -m "fix(auth): 修复认证绕过漏洞"
|
||||
|
||||
# 3. 合并到 master
|
||||
git checkout master
|
||||
git merge --no-ff hotfix/security-fix
|
||||
git tag -a v1.0.1 -m "hotfix: 修复认证绕过漏洞"
|
||||
git push origin master --tags
|
||||
|
||||
# 4. 同步到 develop
|
||||
git checkout develop
|
||||
git merge --no-ff hotfix/security-fix
|
||||
git push origin develop
|
||||
|
||||
# 5. 删除修复分支
|
||||
git branch -d hotfix/security-fix
|
||||
|
||||
# ============================================
|
||||
# 版本发布流程
|
||||
# ============================================
|
||||
|
||||
# 1. 创建发布分支
|
||||
git checkout develop
|
||||
git checkout -b release/v1.0.0
|
||||
|
||||
# 2. 更新版本号和文档
|
||||
# 手动编辑 package.json、CHANGELOG.md 等
|
||||
|
||||
# 3. 提交版本更新
|
||||
git add .
|
||||
git commit -m "chore(release): 准备发布 v1.0.0"
|
||||
|
||||
# 4. 合并到 master
|
||||
git checkout master
|
||||
git merge --no-ff release/v1.0.0
|
||||
git tag -a v1.0.0 -m "release: v1.0.0 正式版本"
|
||||
git push origin master --tags
|
||||
|
||||
# 5. 同步到 develop
|
||||
git checkout develop
|
||||
git merge --no-ff release/v1.0.0
|
||||
git push origin develop
|
||||
|
||||
# 6. 删除发布分支
|
||||
git branch -d release/v1.0.0
|
||||
|
||||
# ============================================
|
||||
# 冲突处理
|
||||
# ============================================
|
||||
|
||||
# Merge 冲突处理
|
||||
git merge feature/xxx
|
||||
# 编辑冲突文件...
|
||||
git add <file>
|
||||
git commit
|
||||
|
||||
# Rebase 冲突处理
|
||||
git rebase develop
|
||||
# 编辑冲突文件...
|
||||
git add <file>
|
||||
git rebase --continue
|
||||
|
||||
# 放弃合并
|
||||
git merge --abort
|
||||
git rebase --abort
|
||||
|
||||
# ============================================
|
||||
# 常用工具命令
|
||||
# ============================================
|
||||
|
||||
# 查看状态
|
||||
git status
|
||||
|
||||
# 查看日志
|
||||
git log --oneline --graph --all
|
||||
|
||||
# 查看分支
|
||||
git branch -a
|
||||
|
||||
# 暂存工作
|
||||
git stash save "工作进行中"
|
||||
git stash list
|
||||
git stash pop
|
||||
|
||||
# 查看文件修改
|
||||
git diff
|
||||
git diff --staged
|
||||
git log -p -- <file>
|
||||
@@ -0,0 +1,393 @@
|
||||
# Git 高级用法
|
||||
|
||||
## 标签管理
|
||||
|
||||
### 版本号规范
|
||||
|
||||
采用 **语义化版本**(Semantic Versioning):
|
||||
|
||||
```
|
||||
主版本号.次版本号.修订号[-预发布标识]
|
||||
MAJOR.MINOR.PATCH[-PRERELEASE]
|
||||
```
|
||||
|
||||
| 版本变化 | 说明 | 示例 |
|
||||
| :------- | :----------------- | :---------------- |
|
||||
| 主版本号 | 不兼容的 API 修改 | `v1.0.0 → v2.0.0` |
|
||||
| 次版本号 | 向下兼容的功能新增 | `v1.0.0 → v1.1.0` |
|
||||
| 修订号 | 向下兼容的问题修正 | `v1.0.0 → v1.0.1` |
|
||||
|
||||
### 预发布标识
|
||||
|
||||
- `alpha` - 内测版本
|
||||
- `beta` - 公测版本
|
||||
- `rc` - 候选版本
|
||||
|
||||
```
|
||||
v1.0.0-alpha.1 # 第一个内测版本
|
||||
v1.0.0-beta.1 # 第一个公测版本
|
||||
v1.0.0-rc.1 # 第一个候选版本
|
||||
v1.0.0 # 正式版本
|
||||
```
|
||||
|
||||
### 标签操作
|
||||
|
||||
#### 创建附注标签(推荐)
|
||||
|
||||
```bash
|
||||
git tag -a v1.0.0 -m "release: v1.0.0 正式版本
|
||||
|
||||
主要更新:
|
||||
- 新增用户管理模块
|
||||
- 新增支付功能
|
||||
- 优化查询性能"
|
||||
```
|
||||
|
||||
#### 推送标签
|
||||
|
||||
```bash
|
||||
# 推送单个标签
|
||||
git push origin v1.0.0
|
||||
|
||||
# 推送所有标签
|
||||
git push origin --tags
|
||||
```
|
||||
|
||||
#### 查看标签
|
||||
|
||||
```bash
|
||||
git tag
|
||||
git tag -l "v1.*"
|
||||
git show v1.0.0
|
||||
```
|
||||
|
||||
#### 删除标签
|
||||
|
||||
```bash
|
||||
# 删除本地标签
|
||||
git tag -d v1.0.0
|
||||
|
||||
# 删除远程标签
|
||||
git push origin :refs/tags/v1.0.0
|
||||
```
|
||||
|
||||
## Git 性能优化
|
||||
|
||||
### 大型仓库优化
|
||||
|
||||
```bash
|
||||
# 浅克隆(只获取最近的提交)
|
||||
git clone --depth 1 https://github.com/repo/project.git
|
||||
|
||||
# 部分克隆(按需获取)
|
||||
git clone --filter=blob:none https://github.com/repo/project.git
|
||||
|
||||
# 稀疏检出(只检出需要的目录)
|
||||
git clone --filter=blob:none --sparse https://github.com/repo/project.git
|
||||
cd project
|
||||
git sparse-checkout init --cone
|
||||
git sparse-checkout set src/frontend
|
||||
```
|
||||
|
||||
### 清理仓库
|
||||
|
||||
```bash
|
||||
# 查看仓库大小
|
||||
git count-objects -vH
|
||||
|
||||
# 清理无用对象
|
||||
git gc --aggressive --prune=now
|
||||
|
||||
# 清理远程已删除的分支引用
|
||||
git remote prune origin
|
||||
|
||||
# 清理本地已合并的分支
|
||||
git branch --merged master | grep -v "\\*\\|master\\|develop" | xargs -n 1 git branch -d
|
||||
```
|
||||
|
||||
### 提升操作速度
|
||||
|
||||
```bash
|
||||
# 启用文件系统缓存
|
||||
git config --global core.fscache true
|
||||
|
||||
# 启用并行获取
|
||||
git config --global fetch.parallel 4
|
||||
|
||||
# 启用未跟踪文件缓存
|
||||
git config --global core.untrackedCache true
|
||||
```
|
||||
|
||||
## Git 安全规范
|
||||
|
||||
### 敏感信息保护
|
||||
|
||||
```bash
|
||||
# 检查历史提交中的敏感信息
|
||||
git log -p | grep -E "(password|secret|api_key)"
|
||||
|
||||
# 从历史记录中删除敏感文件
|
||||
git filter-branch --force --index-filter \
|
||||
'git rm --cached --ignore-unmatch config/secrets.yml' \
|
||||
--prune-empty --tag-name-filter cat -- --all
|
||||
|
||||
# 使用 git-secrets 预防敏感信息提交
|
||||
git secrets --install
|
||||
git secrets --register-aws
|
||||
```
|
||||
|
||||
### 签名验证
|
||||
|
||||
```bash
|
||||
# 配置 GPG 签名
|
||||
git config --global user.signingkey YOUR_KEY_ID
|
||||
git config --global commit.gpgsign true
|
||||
|
||||
# 创建签名提交
|
||||
git commit -S -m "feat: 签名提交"
|
||||
|
||||
# 验证签名
|
||||
git log --show-signature
|
||||
```
|
||||
|
||||
### 仓库权限控制
|
||||
|
||||
| 规则 | master | develop | feature/* |
|
||||
| :--------------- | :----- | :------ | :-------- |
|
||||
| 禁止强制推送 | ✅ | ✅ | ❌ |
|
||||
| 禁止删除 | ✅ | ✅ | ❌ |
|
||||
| 必须 Code Review | ✅ | ✅ | ❌ |
|
||||
| 必须通过 CI | ✅ | ✅ | ❌ |
|
||||
| 必须签名提交 | ✅ | ❌ | ❌ |
|
||||
|
||||
## 子模块管理
|
||||
|
||||
### 添加子模块
|
||||
|
||||
```bash
|
||||
git submodule add https://github.com/user/repo.git libs/repo
|
||||
|
||||
# 克隆包含子模块的项目
|
||||
git clone --recurse-submodules https://github.com/user/project.git
|
||||
|
||||
# 初始化已有项目的子模块
|
||||
git submodule init
|
||||
git submodule update
|
||||
```
|
||||
|
||||
### 更新子模块
|
||||
|
||||
```bash
|
||||
# 更新单个子模块
|
||||
cd libs/repo
|
||||
git pull origin main
|
||||
|
||||
# 更新所有子模块
|
||||
git submodule update --remote
|
||||
|
||||
# 提交子模块更新
|
||||
cd ..
|
||||
git add libs/repo
|
||||
git commit -m "chore: 更新子模块版本"
|
||||
```
|
||||
|
||||
### 删除子模块
|
||||
|
||||
```bash
|
||||
# 删除子模块条目
|
||||
git submodule deinit -f libs/repo
|
||||
|
||||
# 删除 .git/modules 中的缓存
|
||||
rm -rf .git/modules/libs/repo
|
||||
|
||||
# 删除子模块目录
|
||||
git rm -f libs/repo
|
||||
```
|
||||
|
||||
## 常见问题解决
|
||||
|
||||
### 1. 修改最后一次提交
|
||||
|
||||
```bash
|
||||
# 修改提交内容(未推送)
|
||||
git add forgotten-file.ts
|
||||
git commit --amend --no-edit
|
||||
|
||||
# 修改提交消息
|
||||
git commit --amend -m "新的提交消息"
|
||||
|
||||
# 回滚最后一次提交,保留更改
|
||||
git reset --soft HEAD~1
|
||||
```
|
||||
|
||||
### 2. 推送被拒绝
|
||||
|
||||
```bash
|
||||
# 先拉取再推送
|
||||
git pull origin master
|
||||
git push origin master
|
||||
|
||||
# 使用 rebase 保持历史清晰
|
||||
git pull --rebase origin master
|
||||
git push origin master
|
||||
```
|
||||
|
||||
### 3. 回滚到之前版本
|
||||
|
||||
```bash
|
||||
# 重置到指定提交(丢弃之后的提交)
|
||||
git reset --hard abc123
|
||||
|
||||
# 创建反向提交(推荐,保留历史)
|
||||
git revert abc123
|
||||
```
|
||||
|
||||
### 4. 恢复误删的分支
|
||||
|
||||
```bash
|
||||
# 查看操作历史
|
||||
git reflog
|
||||
|
||||
# 恢复分支
|
||||
git checkout -b feature/xxx def456
|
||||
```
|
||||
|
||||
### 5. 合并多个提交
|
||||
|
||||
```bash
|
||||
# 交互式 rebase(只能合并未推送的提交)
|
||||
git rebase -i HEAD~5
|
||||
|
||||
# 在编辑器中将要合并的提交标记为 squash
|
||||
```
|
||||
|
||||
### 6. 暂存当前工作
|
||||
|
||||
```bash
|
||||
git stash save "工作进行中"
|
||||
git stash list
|
||||
git stash pop
|
||||
git stash apply stash@{0}
|
||||
```
|
||||
|
||||
### 7. 查看文件修改历史
|
||||
|
||||
```bash
|
||||
git log -- <file> # 提交历史
|
||||
git log -p -- <file> # 详细内容
|
||||
git blame <file> # 每行修改人
|
||||
```
|
||||
|
||||
### 8. 处理大文件
|
||||
|
||||
```bash
|
||||
# 使用 Git LFS
|
||||
git lfs install
|
||||
git lfs track "*.zip"
|
||||
git add .gitattributes
|
||||
```
|
||||
|
||||
## 实用技巧
|
||||
|
||||
### 配置别名
|
||||
|
||||
```bash
|
||||
git config --global alias.co checkout
|
||||
git config --global alias.br branch
|
||||
git config --global alias.ci commit
|
||||
git config --global alias.st status
|
||||
git config --global alias.lg "log --graph --oneline --all"
|
||||
```
|
||||
|
||||
### 美化日志
|
||||
|
||||
```bash
|
||||
# 图形化历史
|
||||
git log --graph --oneline --all
|
||||
|
||||
# 搜索提交消息
|
||||
git log --grep="用户管理"
|
||||
|
||||
# 搜索代码变更
|
||||
git log -S"function_name"
|
||||
```
|
||||
|
||||
### 快速操作
|
||||
|
||||
```bash
|
||||
# 放弃所有未提交更改
|
||||
git reset --hard HEAD
|
||||
|
||||
# 放弃某个文件更改
|
||||
git checkout -- filename
|
||||
|
||||
# 删除未跟踪文件
|
||||
git clean -fd
|
||||
|
||||
# 批量删除已合并分支
|
||||
git branch --merged master | grep -v "\* master" | xargs -n 1 git branch -d
|
||||
```
|
||||
|
||||
### 安全操作
|
||||
|
||||
```bash
|
||||
# 查看将要推送的内容
|
||||
git push --dry-run
|
||||
|
||||
# 安全的强制推送
|
||||
git push --force-with-lease
|
||||
|
||||
# 备份分支
|
||||
git branch backup-master master
|
||||
```
|
||||
|
||||
### 查找问题提交
|
||||
|
||||
```bash
|
||||
# 二分查找引入Bug的提交
|
||||
git bisect start
|
||||
git bisect bad # 标记当前有问题
|
||||
git bisect good v1.0.0 # 标记某版本是好的
|
||||
# Git会自动切换提交,测试后标记 good/bad
|
||||
git bisect reset # 结束查找
|
||||
```
|
||||
|
||||
## CHANGELOG 管理
|
||||
|
||||
### 自动生成 CHANGELOG
|
||||
|
||||
使用 `conventional-changelog` 自动生成:
|
||||
|
||||
```bash
|
||||
# 安装
|
||||
pnpm install -D conventional-changelog-cli
|
||||
|
||||
# 生成 CHANGELOG
|
||||
npx conventional-changelog -p angular -i CHANGELOG.md -s
|
||||
```
|
||||
|
||||
### CHANGELOG 格式
|
||||
|
||||
```markdown
|
||||
# 更新日志
|
||||
|
||||
## [1.2.0] - 2024-01-15
|
||||
|
||||
### 新增
|
||||
- 新增用户导出功能 (#123)
|
||||
- 新增数据备份模块
|
||||
|
||||
### 修复
|
||||
- 修复登录验证码不刷新问题 (#456)
|
||||
- 修复列表分页异常
|
||||
|
||||
### 变更
|
||||
- 优化用户查询性能
|
||||
- 调整菜单权限校验逻辑
|
||||
|
||||
### 移除
|
||||
- 移除废弃的API接口
|
||||
|
||||
## [1.1.0] - 2024-01-01
|
||||
...
|
||||
```
|
||||
@@ -0,0 +1,175 @@
|
||||
# 分支管理策略详解
|
||||
|
||||
## 分支类型
|
||||
|
||||
| 分支类型 | 命名规范 | 说明 | 生命周期 |
|
||||
| :------- | :---------------- | :------------------------- | :------------- |
|
||||
| master | `master` | 主分支,始终保持可发布状态 | 永久 |
|
||||
| develop | `develop` | 开发分支,集成最新开发代码 | 永久 |
|
||||
| feature | `feature/功能名` | 功能分支 | 开发完成后删除 |
|
||||
| bugfix | `bugfix/问题描述` | Bug修复分支 | 修复完成后删除 |
|
||||
| hotfix | `hotfix/问题描述` | 紧急修复分支 | 修复完成后删除 |
|
||||
| release | `release/版本号` | 发布分支 | 发布完成后删除 |
|
||||
|
||||
## 分支命名规范
|
||||
|
||||
### 功能分支
|
||||
|
||||
```
|
||||
feature/user-management # ✅ 用户管理功能
|
||||
feature/123-add-export # ✅ 关联Issue的功能
|
||||
```
|
||||
|
||||
### Bug修复分支
|
||||
|
||||
```
|
||||
bugfix/login-error # ✅ 登录错误修复
|
||||
bugfix/456-fix-timeout # ✅ 关联Issue的修复
|
||||
```
|
||||
|
||||
### 紧急修复分支
|
||||
|
||||
```
|
||||
hotfix/security-vulnerability # ✅ 安全漏洞修复
|
||||
hotfix/v1.0.1 # ✅ 版本号修复
|
||||
```
|
||||
|
||||
### 发布分支
|
||||
|
||||
```
|
||||
release/v1.0.0 # ✅ 版本发布
|
||||
release/v2.0.0-beta.1 # ✅ 预发布版本
|
||||
```
|
||||
|
||||
## 分支保护规则
|
||||
|
||||
### master 分支
|
||||
|
||||
- 禁止直接推送
|
||||
- 必须通过 Pull Request 合并
|
||||
- 必须通过 CI 检查
|
||||
- 必须至少一人 Code Review
|
||||
|
||||
### develop 分支
|
||||
|
||||
- 限制直接推送
|
||||
- 建议通过 Pull Request 合并
|
||||
- 必须通过 CI 检查
|
||||
|
||||
## 分支操作命令
|
||||
|
||||
### 创建功能分支
|
||||
|
||||
```bash
|
||||
git checkout develop
|
||||
git pull origin develop
|
||||
git checkout -b feature/user-management
|
||||
```
|
||||
|
||||
### 创建Bug修复分支
|
||||
|
||||
```bash
|
||||
git checkout develop
|
||||
git pull origin develop
|
||||
git checkout -b bugfix/login-error
|
||||
```
|
||||
|
||||
### 创建紧急修复分支(从master创建)
|
||||
|
||||
```bash
|
||||
git checkout master
|
||||
git pull origin master
|
||||
git checkout -b hotfix/security-fix
|
||||
```
|
||||
|
||||
### 删除分支
|
||||
|
||||
```bash
|
||||
git branch -d feature/user-management # 删除本地分支
|
||||
git push origin -d feature/user-management # 删除远程分支
|
||||
```
|
||||
|
||||
## 工作流程详解
|
||||
|
||||
### 日常开发流程
|
||||
|
||||
```bash
|
||||
# 1. 同步最新代码
|
||||
git checkout develop
|
||||
git pull origin develop
|
||||
|
||||
# 2. 创建功能分支
|
||||
git checkout -b feature/user-management
|
||||
|
||||
# 3. 开发并提交
|
||||
git add .
|
||||
git commit -m "feat(user): 添加用户列表页面"
|
||||
|
||||
# 4. 推送到远程
|
||||
git push -u origin feature/user-management
|
||||
|
||||
# 5. 创建 Pull Request 并请求 Code Review
|
||||
|
||||
# 6. 合并到 develop(通过 PR)
|
||||
|
||||
# 7. 删除功能分支
|
||||
git branch -d feature/user-management
|
||||
git push origin -d feature/user-management
|
||||
```
|
||||
|
||||
### 紧急修复流程
|
||||
|
||||
```bash
|
||||
# 1. 从 master 创建修复分支
|
||||
git checkout master
|
||||
git pull origin master
|
||||
git checkout -b hotfix/critical-bug
|
||||
|
||||
# 2. 修复并提交
|
||||
git add .
|
||||
git commit -m "fix(auth): 修复认证绕过漏洞"
|
||||
|
||||
# 3. 合并到 master
|
||||
git checkout master
|
||||
git merge --no-ff hotfix/critical-bug
|
||||
git tag -a v1.0.1 -m "hotfix: 修复认证绕过漏洞"
|
||||
git push origin master --tags
|
||||
|
||||
# 4. 同步到 develop
|
||||
git checkout develop
|
||||
git merge --no-ff hotfix/critical-bug
|
||||
git push origin develop
|
||||
|
||||
# 5. 删除修复分支
|
||||
git branch -d hotfix/critical-bug
|
||||
```
|
||||
|
||||
### 版本发布流程
|
||||
|
||||
```bash
|
||||
# 1. 创建发布分支
|
||||
git checkout develop
|
||||
git checkout -b release/v1.0.0
|
||||
|
||||
# 2. 更新版本号和文档
|
||||
# 修改 package.json 版本号
|
||||
# 更新 CHANGELOG.md
|
||||
|
||||
# 3. 提交版本更新
|
||||
git add .
|
||||
git commit -m "chore(release): 准备发布 v1.0.0"
|
||||
|
||||
# 4. 合并到 master
|
||||
git checkout master
|
||||
git merge --no-ff release/v1.0.0
|
||||
git tag -a v1.0.0 -m "release: v1.0.0 正式版本"
|
||||
git push origin master --tags
|
||||
|
||||
# 5. 同步到 develop
|
||||
git checkout develop
|
||||
git merge --no-ff release/v1.0.0
|
||||
git push origin develop
|
||||
|
||||
# 6. 删除发布分支
|
||||
git branch -d release/v1.0.0
|
||||
```
|
||||
@@ -0,0 +1,119 @@
|
||||
# 多人协作规范
|
||||
|
||||
## Pull Request 规范
|
||||
|
||||
创建 PR 时应包含以下内容:
|
||||
|
||||
```markdown
|
||||
## 变更说明
|
||||
<!-- 描述本次变更的内容和目的 -->
|
||||
|
||||
## 变更类型
|
||||
- [ ] 新功能 (feat)
|
||||
- [ ] Bug 修复 (fix)
|
||||
- [ ] 代码重构 (refactor)
|
||||
- [ ] 文档更新 (docs)
|
||||
- [ ] 其他
|
||||
|
||||
## 测试方式
|
||||
<!-- 描述如何测试这些变更 -->
|
||||
|
||||
## 关联 Issue
|
||||
Closes #xxx
|
||||
|
||||
## 检查清单
|
||||
- [ ] 代码已自测
|
||||
- [ ] 文档已更新
|
||||
- [ ] 变更已添加到 CHANGELOG
|
||||
```
|
||||
|
||||
## Code Review 规范
|
||||
|
||||
### 审查要点
|
||||
|
||||
#### 1. 代码质量
|
||||
|
||||
- 代码是否清晰易读
|
||||
- 命名是否规范
|
||||
- 是否有重复代码
|
||||
|
||||
#### 2. 逻辑正确性
|
||||
|
||||
- 业务逻辑是否正确
|
||||
- 边界条件是否处理
|
||||
- 异常情况是否考虑
|
||||
|
||||
#### 3. 安全性
|
||||
|
||||
- 是否有安全漏洞
|
||||
- 敏感信息是否暴露
|
||||
- 输入是否校验
|
||||
|
||||
#### 4. 性能
|
||||
|
||||
- 是否有性能问题
|
||||
- 资源是否正确释放
|
||||
- 算法复杂度是否合理
|
||||
|
||||
### 反馈格式
|
||||
|
||||
```markdown
|
||||
<!-- 必须修改 -->
|
||||
🔴 **必须修改**: 这里有 SQL 注入风险,需要使用参数化查询
|
||||
|
||||
<!-- 建议修改 -->
|
||||
🟡 **建议修改**: 这个方法可以提取为工具函数,提高复用性
|
||||
|
||||
<!-- 讨论 -->
|
||||
💬 **讨论**: 这里是否可以考虑使用缓存?
|
||||
|
||||
<!-- 赞 -->
|
||||
👍 **赞**: 这个封装很优雅!
|
||||
```
|
||||
|
||||
## 最佳实践总结
|
||||
|
||||
### 提交规范
|
||||
|
||||
✅ **推荐:**
|
||||
|
||||
- 使用 Conventional Commits 规范
|
||||
- 提交消息清晰描述改动
|
||||
- 一次提交只做一件事
|
||||
- 提交前进行代码检查
|
||||
|
||||
❌ **禁止:**
|
||||
|
||||
- 提交消息模糊不清
|
||||
- 一次提交多个不相关改动
|
||||
- 提交敏感信息(密码、密钥)
|
||||
- 直接在主分支开发
|
||||
|
||||
### 分支管理
|
||||
|
||||
✅ **推荐:**
|
||||
|
||||
- 使用 feature 分支开发
|
||||
- 定期同步主分支代码
|
||||
- 功能完成后及时删除分支
|
||||
- 使用 `--no-ff` 合并保留历史
|
||||
|
||||
❌ **禁止:**
|
||||
|
||||
- 在主分支直接开发
|
||||
- 长期不合并的功能分支
|
||||
- 分支命名不规范
|
||||
- 在公共分支上 rebase
|
||||
|
||||
### 代码审查
|
||||
|
||||
✅ **推荐:**
|
||||
|
||||
- 所有代码通过 Pull Request
|
||||
- 至少一人审核通过才能合并
|
||||
- 提供建设性反馈
|
||||
|
||||
❌ **禁止:**
|
||||
|
||||
- 未经审查直接合并
|
||||
- 自己审查自己的代码
|
||||
@@ -0,0 +1,128 @@
|
||||
# Commit Message 详细规范
|
||||
|
||||
## Conventional Commits 格式
|
||||
|
||||
采用 **Conventional Commits** 规范,提交消息格式如下:
|
||||
|
||||
```
|
||||
<type>(<scope>): <subject>
|
||||
|
||||
<body>
|
||||
|
||||
<footer>
|
||||
```
|
||||
|
||||
## 字段说明
|
||||
|
||||
| 字段 | 必填 | 说明 |
|
||||
| :------ | :--- | :------- |
|
||||
| type | ✅ | 提交类型 |
|
||||
| scope | ❌ | 影响范围 |
|
||||
| subject | ✅ | 简短描述 |
|
||||
| body | ❌ | 详细描述 |
|
||||
| footer | ❌ | 脚注信息 |
|
||||
|
||||
## Type 类型
|
||||
|
||||
| 类型 | 说明 | 示例 |
|
||||
| :--------- | :------- | :---------------------------------- |
|
||||
| `feat` | 新功能 | `feat(user): 新增用户导出功能` |
|
||||
| `fix` | 修复Bug | `fix(login): 修复验证码不刷新问题` |
|
||||
| `docs` | 文档更新 | `docs(api): 更新接口文档` |
|
||||
| `style` | 代码格式 | `style: 调整代码缩进` |
|
||||
| `refactor` | 重构 | `refactor(utils): 重构日期工具函数` |
|
||||
| `perf` | 性能优化 | `perf(list): 优化列表渲染性能` |
|
||||
| `test` | 测试相关 | `test(user): 添加用户模块单元测试` |
|
||||
| `build` | 构建相关 | `build: 升级 vite 到 5.0` |
|
||||
| `ci` | CI配置 | `ci: 添加 GitHub Actions` |
|
||||
| `chore` | 其他修改 | `chore: 更新依赖版本` |
|
||||
| `revert` | 回滚提交 | `revert: 回滚 feat(user)` |
|
||||
|
||||
## Scope 范围
|
||||
|
||||
Scope 用于说明提交影响的范围,常用的 scope 包括:
|
||||
|
||||
- `data` - 数据处理
|
||||
- `utils` - 工具函数
|
||||
- `model` - 模型架构
|
||||
- `config` - 参数配置
|
||||
- `trainer` - 训练
|
||||
- `evaluator` - 测评
|
||||
- `workflow` - 工作流
|
||||
|
||||
## Subject 规范
|
||||
|
||||
- 使用动词开头:添加、修复、更新、移除、优化
|
||||
- 不超过50个字符
|
||||
- 不以句号结尾
|
||||
- 使用中文或英文,保持一致
|
||||
|
||||
### 正确与错误示例
|
||||
|
||||
```
|
||||
# ✅ 正确示例
|
||||
feat(user): 添加用户导出功能
|
||||
fix(login): 修复验证码不刷新问题
|
||||
|
||||
# ❌ 错误示例
|
||||
feat(user): 添加用户导出功能。 # 不要句号
|
||||
feat(user): 用户导出 # 要用动词开头
|
||||
feat: 添加了一个新的用户导出功能 # 太长
|
||||
```
|
||||
|
||||
## Body 详细描述
|
||||
|
||||
当改动较大或需要说明原因时,使用 Body 提供详细描述:
|
||||
|
||||
```
|
||||
feat(user): 添加用户批量导入功能
|
||||
|
||||
- 支持 Excel 文件导入
|
||||
- 支持数据校验和错误提示
|
||||
- 支持导入进度显示
|
||||
|
||||
相关需求: #123
|
||||
```
|
||||
|
||||
## Footer 脚注
|
||||
|
||||
用于关联 Issue 或说明破坏性变更:
|
||||
|
||||
```
|
||||
# 关联 Issue
|
||||
Closes #123, #456
|
||||
|
||||
# 破坏性变更
|
||||
BREAKING CHANGE: 用户接口返回格式变更
|
||||
旧格式: { data: user }
|
||||
新格式: { code: 200, data: user, msg: 'success' }
|
||||
```
|
||||
|
||||
## 完整示例
|
||||
|
||||
### 简单提交
|
||||
|
||||
```bash
|
||||
git commit -m "feat(user): 添加用户导出功能"
|
||||
```
|
||||
|
||||
### 带 Body 的提交
|
||||
|
||||
```bash
|
||||
git commit -m "fix(login): 修复验证码不刷新问题
|
||||
|
||||
原因: 缓存时间设置过长导致验证码一直显示同一张图片
|
||||
方案: 将缓存时间从5分钟调整为1分钟"
|
||||
```
|
||||
|
||||
### 带 Footer 的提交
|
||||
|
||||
```bash
|
||||
git commit -m "feat(api): 重构用户接口
|
||||
|
||||
BREAKING CHANGE: 用户查询接口路径变更
|
||||
旧路径: /api/user/list
|
||||
新路径: /api/system/user/list
|
||||
|
||||
Closes #789"
|
||||
```
|
||||
@@ -0,0 +1,148 @@
|
||||
# 冲突处理详解
|
||||
|
||||
## 识别冲突
|
||||
|
||||
当 Git 无法自动合并时,会标记冲突:
|
||||
|
||||
```
|
||||
<<<<<<< HEAD
|
||||
// 当前分支的代码
|
||||
const name = '张三'
|
||||
=======
|
||||
// 要合并的分支的代码
|
||||
const name = '李四'
|
||||
>>>>>>> feature/user-management
|
||||
```
|
||||
|
||||
## 解决冲突步骤
|
||||
|
||||
### 1. 查看冲突文件
|
||||
|
||||
```bash
|
||||
git status
|
||||
```
|
||||
|
||||
### 2. 手动编辑文件,解决冲突
|
||||
|
||||
打开冲突文件,找到冲突标记(`<<<<<<<`、`=======`、`>>>>>>>`),手动编辑选择保留的内容或合并两者。
|
||||
|
||||
### 3. 标记已解决
|
||||
|
||||
```bash
|
||||
git add <file>
|
||||
```
|
||||
|
||||
### 4. 完成合并
|
||||
|
||||
```bash
|
||||
# 对于 merge 冲突
|
||||
git commit
|
||||
|
||||
# 对于 rebase 冲突
|
||||
git rebase --continue
|
||||
```
|
||||
|
||||
## 冲突处理策略
|
||||
|
||||
### 保留当前分支版本
|
||||
|
||||
```bash
|
||||
git checkout --ours <file>
|
||||
git add <file>
|
||||
```
|
||||
|
||||
### 保留传入分支版本
|
||||
|
||||
```bash
|
||||
git checkout --theirs <file>
|
||||
git add <file>
|
||||
```
|
||||
|
||||
### 放弃合并
|
||||
|
||||
```bash
|
||||
# 放弃 merge
|
||||
git merge --abort
|
||||
|
||||
# 放弃 rebase
|
||||
git rebase --abort
|
||||
```
|
||||
|
||||
## 预防冲突的最佳实践
|
||||
|
||||
1. **及时同步代码** - 每天开始工作前拉取最新代码
|
||||
2. **小步提交** - 频繁提交小的改动
|
||||
3. **功能模块化** - 不同功能在不同文件中实现
|
||||
4. **沟通协作** - 避免同时修改同一文件
|
||||
|
||||
## 常见冲突场景
|
||||
|
||||
### 场景1:同一文件不同位置修改
|
||||
|
||||
这种情况 Git 通常能自动合并,无需人工干预。
|
||||
|
||||
### 场景2:同一行不同修改
|
||||
|
||||
需要手动决定保留哪个版本或合并两者。
|
||||
|
||||
### 场景3:文件重命名
|
||||
|
||||
Git 通常能智能识别,但如果一个分支重命名而另一个分支修改内容,可能需要手动处理。
|
||||
|
||||
### 场景4:二进制文件冲突
|
||||
|
||||
对于图片、PDF 等二进制文件,需要决定保留哪个版本:
|
||||
|
||||
```bash
|
||||
# 保留当前分支的版本
|
||||
git checkout --ours image.png
|
||||
|
||||
# 或保留传入分支的版本
|
||||
git checkout --theirs image.png
|
||||
```
|
||||
|
||||
## 冲突解决工具
|
||||
|
||||
### 使用 merge 工具
|
||||
|
||||
```bash
|
||||
# 配置合并工具
|
||||
git config --global merge.tool vimdiff
|
||||
git config --global mergetool.prompt false
|
||||
|
||||
# 使用合并工具
|
||||
git mergetool
|
||||
```
|
||||
|
||||
### 使用 diff 工具
|
||||
|
||||
```bash
|
||||
# 查看详细差异
|
||||
git diff --ours
|
||||
git diff --theirs
|
||||
git diff --base
|
||||
```
|
||||
|
||||
## Rebase 冲突特殊处理
|
||||
|
||||
Rebase 时冲突会逐个提交出现,处理方式:
|
||||
|
||||
```bash
|
||||
git rebase develop
|
||||
# 冲突1 -> 解决 -> git add -> git rebase --continue
|
||||
# 冲突2 -> 解决 -> git add -> git rebase --continue
|
||||
# ...
|
||||
# 直到完成
|
||||
```
|
||||
|
||||
如果某一步想跳过:
|
||||
|
||||
```bash
|
||||
git rebase --skip
|
||||
```
|
||||
|
||||
如果整体想放弃:
|
||||
|
||||
```bash
|
||||
git rebase --abort
|
||||
```
|
||||
@@ -0,0 +1,276 @@
|
||||
# .gitignore 规范
|
||||
|
||||
## 基本规则
|
||||
|
||||
```
|
||||
# 空行:不匹配任何文件
|
||||
# 注释:以 # 开头
|
||||
# 目录:以 / 结尾
|
||||
# 取反:以 ! 开头表示不忽略
|
||||
# 根目录:以 / 开头表示项目根目录
|
||||
|
||||
*.log # 忽略所有 .log 文件
|
||||
node_modules/ # 忽略 node_modules 目录
|
||||
/temp/ # 忽略根目录下的 temp 目录
|
||||
**/.env # 忽略所有目录下的 .env 文件
|
||||
!.gitkeep # 不忽略 .gitkeep 文件
|
||||
```
|
||||
|
||||
## 通用 .gitignore
|
||||
|
||||
```
|
||||
# ============================================
|
||||
# 依赖目录
|
||||
# ============================================
|
||||
node_modules/
|
||||
vendor/
|
||||
|
||||
# ============================================
|
||||
# 构建产物
|
||||
# ============================================
|
||||
dist/
|
||||
build/
|
||||
target/
|
||||
|
||||
# ============================================
|
||||
# 编辑器和 IDE
|
||||
# ============================================
|
||||
.idea/
|
||||
.vscode/
|
||||
*.sw?
|
||||
|
||||
# ============================================
|
||||
# 环境配置
|
||||
# ============================================
|
||||
.env
|
||||
.env.local
|
||||
.env.*.local
|
||||
|
||||
# ============================================
|
||||
# 日志文件
|
||||
# ============================================
|
||||
logs/
|
||||
*.log
|
||||
npm-debug.log*
|
||||
|
||||
# ============================================
|
||||
# 系统文件
|
||||
# ============================================
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
# ============================================
|
||||
# 缓存文件
|
||||
# ============================================
|
||||
.cache/
|
||||
.eslintcache
|
||||
.stylelintcache
|
||||
```
|
||||
|
||||
## 项目特定配置
|
||||
|
||||
### 前端/文档项目补充
|
||||
|
||||
```
|
||||
# VitePress
|
||||
docs/.vitepress/dist
|
||||
docs/.vitepress/cache
|
||||
|
||||
# Node.js
|
||||
package-lock.json
|
||||
yarn.lock
|
||||
pnpm-lock.yaml
|
||||
```
|
||||
|
||||
### 后端项目补充
|
||||
|
||||
```
|
||||
# Maven
|
||||
target/
|
||||
pom.xml.tag
|
||||
*.jar
|
||||
!**/src/main/**/target/
|
||||
|
||||
# 敏感配置
|
||||
application-local.yml
|
||||
application-dev.yml
|
||||
```
|
||||
|
||||
### Python 项目补充
|
||||
|
||||
```
|
||||
# Python
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
*.so
|
||||
.Python
|
||||
venv/
|
||||
env/
|
||||
ENV/
|
||||
.pytest_cache/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
# pytype
|
||||
.pytype/
|
||||
```
|
||||
|
||||
### Go 项目补充
|
||||
|
||||
```
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool
|
||||
*.out
|
||||
|
||||
# Dependency directories
|
||||
vendor/
|
||||
|
||||
# Go workspace file
|
||||
go.work
|
||||
```
|
||||
|
||||
### Rust 项目补充
|
||||
|
||||
```
|
||||
# Rust
|
||||
/target/
|
||||
**/*.rs.bk
|
||||
*.pdb
|
||||
Cargo.lock
|
||||
```
|
||||
|
||||
## .gitignore 技巧
|
||||
|
||||
### 忽略文件但保留目录
|
||||
|
||||
```
|
||||
logs/*
|
||||
!logs/.gitkeep
|
||||
```
|
||||
|
||||
### 检查忽略规则
|
||||
|
||||
```bash
|
||||
git check-ignore -v filename
|
||||
```
|
||||
|
||||
### 清理已提交的忽略文件
|
||||
|
||||
```bash
|
||||
git rm --cached filename
|
||||
git commit -m "chore: 移除不应提交的文件"
|
||||
```
|
||||
|
||||
### 调试 .gitignore
|
||||
|
||||
```bash
|
||||
# 查看文件是否被忽略以及被哪条规则匹配
|
||||
git check-ignore -v path/to/file
|
||||
|
||||
# 查看所有被忽略的文件
|
||||
git ls-files --others --ignored --exclude-standard
|
||||
```
|
||||
|
||||
## 常见模式
|
||||
|
||||
### 忽略特定文件
|
||||
|
||||
```
|
||||
# 忽略特定文件
|
||||
config/local.json
|
||||
secrets.yaml
|
||||
```
|
||||
|
||||
### 忽略特定类型
|
||||
|
||||
```
|
||||
# 忽略所有 .log 文件
|
||||
*.log
|
||||
|
||||
# 忽略所有临时文件
|
||||
*.tmp
|
||||
*.temp
|
||||
```
|
||||
|
||||
### 忽略目录
|
||||
|
||||
```
|
||||
# 忽略所有 node_modules 目录
|
||||
node_modules/
|
||||
|
||||
# 忽略根目录下的 build 目录
|
||||
/build/
|
||||
|
||||
# 忽略任何位置的 build 目录
|
||||
**/build/
|
||||
```
|
||||
|
||||
### 取反规则
|
||||
|
||||
```
|
||||
# 忽略所有 .a 文件
|
||||
*.a
|
||||
|
||||
# 但不忽略 lib.a
|
||||
!lib.a
|
||||
|
||||
# 忽略所有 TODO 文件
|
||||
TODO*
|
||||
|
||||
# 但不忽略 TODO.md
|
||||
!TODO.md
|
||||
```
|
||||
|
||||
### 通配符
|
||||
|
||||
```
|
||||
# * 匹配任意字符
|
||||
*.log
|
||||
|
||||
# ** 匹配任意目录
|
||||
**/temp/
|
||||
|
||||
# ? 匹配单个字符
|
||||
file?.txt
|
||||
|
||||
# [] 匹配括号内任意字符
|
||||
file[0-9].txt
|
||||
```
|
||||
|
||||
## .gitignore 优先级
|
||||
|
||||
1. 命令行指定的文件(如 `git add -f`)
|
||||
2. `.git/info/exclude`(本地排除规则)
|
||||
3. `.gitignore`(项目级别,提交到仓库)
|
||||
4. `~/.gitignore_global`(全局级别)
|
||||
|
||||
### 本地排除规则
|
||||
|
||||
对于不想提交到仓库的本地忽略规则:
|
||||
|
||||
```bash
|
||||
# 编辑本地排除文件
|
||||
git config --global core.excludesfile ~/.gitignore_global
|
||||
|
||||
# 或者使用 .git/info/exclude(仅当前仓库)
|
||||
echo "secrets.yaml" >> .git/info/exclude
|
||||
```
|
||||
@@ -0,0 +1,121 @@
|
||||
# 合并策略详解
|
||||
|
||||
## Merge vs Rebase
|
||||
|
||||
| 特性 | Merge | Rebase |
|
||||
| :------- | :------------------------- | :----------------------- |
|
||||
| 历史记录 | 保留完整历史,创建合并提交 | 线性历史,不创建合并提交 |
|
||||
| 适用场景 | 公共分支、需要保留历史 | 私有分支、保持历史清晰 |
|
||||
| 冲突处理 | 一次性处理所有冲突 | 逐个提交处理冲突 |
|
||||
| 推荐用法 | 合并到主分支 | 同步上游代码 |
|
||||
|
||||
## 使用建议
|
||||
|
||||
### 功能分支同步 develop:使用 rebase
|
||||
|
||||
```bash
|
||||
git checkout feature/user-management
|
||||
git rebase develop
|
||||
```
|
||||
|
||||
### 功能分支合并到 develop:使用 merge --no-ff
|
||||
|
||||
```bash
|
||||
git checkout develop
|
||||
git merge --no-ff feature/user-management
|
||||
```
|
||||
|
||||
### develop 合并到 master:使用 merge --no-ff
|
||||
|
||||
```bash
|
||||
git checkout master
|
||||
git merge --no-ff develop
|
||||
```
|
||||
|
||||
### 禁止在公共分支上 rebase
|
||||
|
||||
```bash
|
||||
# ❌ 危险操作
|
||||
git checkout develop
|
||||
git rebase feature/xxx # 会改写公共历史
|
||||
```
|
||||
|
||||
## Fast-Forward vs No-Fast-Forward
|
||||
|
||||
### Fast-Forward 合并(不创建合并提交)
|
||||
|
||||
```bash
|
||||
git merge feature/xxx
|
||||
```
|
||||
|
||||
```
|
||||
# A---B---C (master)
|
||||
# \
|
||||
# D---E (feature)
|
||||
# 结果: A---B---C---D---E (master)
|
||||
```
|
||||
|
||||
### No-Fast-Forward 合并(创建合并提交)
|
||||
|
||||
```bash
|
||||
git merge --no-ff feature/xxx
|
||||
```
|
||||
|
||||
```
|
||||
# A---B---C---------M (master)
|
||||
# \ /
|
||||
# D---E (feature)
|
||||
```
|
||||
|
||||
**项目约定**:合并功能分支时使用 `--no-ff`,保留分支历史信息。
|
||||
|
||||
## Squash 合并
|
||||
|
||||
将多个提交压缩成一个:
|
||||
|
||||
```bash
|
||||
git checkout develop
|
||||
git merge --squash feature/user-management
|
||||
git commit -m "feat(user): 添加用户管理功能"
|
||||
```
|
||||
|
||||
### 适用场景
|
||||
|
||||
- 功能分支有太多琐碎提交
|
||||
- 想要保持主分支历史清晰
|
||||
- 不需要保留开发过程中的细节
|
||||
|
||||
### Squash vs Merge --no-ff
|
||||
|
||||
| 策略 | 优点 | 缺点 | 适用场景 |
|
||||
| :---------- | :----------------------- | :----------------------- | :--------------------- |
|
||||
| merge --no-ff | 保留完整历史和开发过程 | 历史可能比较复杂 | 功能分支、重要功能 |
|
||||
| squash | 历史清晰,一个提交一个功能 | 丢失开发过程信息 | 小功能、实验性功能 |
|
||||
| rebase | 线性历史,易于理解 | 改写历史,可能引起问题 | 个人分支、同步上游 |
|
||||
|
||||
## Rebase 高级用法
|
||||
|
||||
### 交互式 Rebase
|
||||
|
||||
```bash
|
||||
# 编辑最近5个提交
|
||||
git rebase -i HEAD~5
|
||||
```
|
||||
|
||||
在编辑器中可以使用以下命令:
|
||||
- `pick` - 保留该提交
|
||||
- `reword` - 修改提交消息
|
||||
- `edit` - 修改提交内容
|
||||
- `squash` - 合并到前一个提交
|
||||
- `drop` - 删除该提交
|
||||
|
||||
### Rebase 时解决冲突
|
||||
|
||||
```bash
|
||||
git rebase develop
|
||||
# 出现冲突后,编辑文件解决冲突
|
||||
git add <file>
|
||||
git rebase --continue
|
||||
# 如果想放弃 rebase
|
||||
git rebase --abort
|
||||
```
|
||||
Reference in New Issue
Block a user