Git Branch Maintenance Guide
This guide defines the Git branch management strategy for the YaoXiang project, with the goal of ensuring orderly codebase development and efficient collaboration.
📋 Table of Contents
- Branch Type Conventions
- Naming Rules
- Branch Lifecycle
- Workflow
- Worktree Parallel Workbench
- Branch Protection Strategy
- Best Practices
- Frequently Asked Questions
🏷️ Branch Type Conventions
Core Branches
| Branch Name | Purpose | Lifecycle | Protection Level |
|---|---|---|---|
main | Production code | Permanent | Strict protection |
dev | Primary development branch | Permanent | Moderate protection |
Feature Branches
| Prefix | Purpose | Naming Examples | Merge Target |
|---|---|---|---|
feature/ | New feature development | feature/type-inferencefeature/ownership-model | dev |
bugfix/ | Fixing known defects | bugfix/memory-leakbugfix/parser-error | dev |
hotfix/ | Urgent production fixes | hotfix/security-patchhotfix/crash-bug | main + dev |
release/ | Release preparation branches | release/v0.8.0release/v1.0.0 | main |
Auxiliary Branches
| Prefix | Purpose | Naming Examples | Merge Target |
|---|---|---|---|
docs/ | Documentation updates | docs/api-referencedocs/tutorial-update | dev |
ci/ | CI/CD configuration changes | ci/add-deploy-scriptci/optimize-build | dev |
refactor/ | Code refactoring | refactor/lexer-optimizationrefactor/memory-manager | dev |
test/ | Test-related changes | test/add-integrationtest/performance-bench | dev |
📝 Naming Rules
Basic Naming Format
# Feature branch
<type>/<short-description>
# Examples
feature/add-type-inference
bugfix/fix-parser-crash
hotfix/security-vulnerabilityNaming Conventions
- Use lowercase letters: Use lowercase letters in all branch names
- Separate words with hyphens: Use
-to separate words; do not use underscores - Use descriptive names: Branch names should clearly communicate their purpose
- Avoid special characters: Do not use spaces, periods, or other special characters
- Length limit: Branch names must not exceed 50 characters
Detailed Examples
# ✅ Good names
feature/user-authentication-system
bugfix/fix-compilation-error-on-windows
hotfix/memory-leak-in-vm
docs/update-api-documentation
refactor/optimize-lexer-performance
test/add-e2e-test-cases
# ❌ Bad names
Feature/NewFeature # Uses uppercase letters
bug_fix # Uses underscores
hotfix/fix # Unclear description
feature/ADD_NEW_FEATURE_WITH_LOTS_OF_DETAILS_THAT_IS_TOO_LONG # Too long🔄 Branch Lifecycle
Branch Creation
# 1. Create from the latest dev branch
git checkout dev
git pull origin dev
git checkout -b feature/your-feature-name
# 2. Push the remote branch
git push -u origin feature/your-feature-nameBranch Development
# Sync the latest code periodically
git checkout dev
git pull origin dev
git checkout feature/your-feature-name
git rebase dev # Or git merge dev
# Commit the code
git add .
git commit -m ":sparkles: feat(frontend): 添加类型推断功能"
git push origin feature/your-feature-nameBranch Merging
# 1. Create a Pull Request
# 2. After the code review is approved
git checkout dev
git pull origin dev
git merge --no-ff feature/your-feature-name
git push origin dev
# 3. Clean up the branches
git branch -d feature/your-feature-name # Delete locally
git push origin --delete feature/your-feature-name # Delete remotelyBranch Deletion
# Delete merged feature branches
git branch -d feature/completed-feature
git push origin --delete feature/completed-feature
# Batch-clean merged branches
git branch --merged dev | grep feature | xargs -n 1 git branch -d🚀 Workflow
Feature Development Workflow
graph TD
A[dev 分支] --> B[创建 feature 分支]
B --> C[开发功能]
C --> D[提交代码]
D --> E[创建 PR 到 dev]
E --> F[代码审查]
F -->|通过| G[合并到 dev]
F -->|拒绝| C
G --> H[删除 feature 分支]
G --> I[CI/CD 触发]Emergency Fix Workflow
graph TD
A[main 分支] --> B[创建 hotfix 分支]
B --> C[修复问题]
C --> D[提交代码]
D --> E[创建 PR 到 main + dev]
E --> F[快速审查]
F --> G[同时合并到 main 和 dev]
G --> H[发布 hotfix]
I[删除 hotfix 分支]Dependabot Security Updates (Always Target main and Are Handled as Emergency Fixes)
GitHub's Dependabot security updates do not read the target-branch setting in .github/dependabot.yml: they are driven by dependency graph alerts on the default branch (main in this repository), and their PRs always target main. Only the weekly version updates continue to target dev as usual. This is existing GitHub behavior, not a misconfiguration (dependabot-core#2767 still does not support retargeting).
Therefore, security update PRs are handled according to the workflow above, and both steps are required:
- Merge it into
main. Do not change the base todev—alert determination is based on the default branch, and changing the base only leaves the alert open; Dependabot may also open another PR againstmainfor the same alert. - On the same day, merge
mainback intodev:
git fetch origin
git switch dev
git merge --ff-only origin/main # Fast-forward directly when dev has no independent commits
git push origin devIf dev has independent commits and cannot be fast-forwarded, use git merge origin/main instead (the resulting history entry is Merge remote-tracking branch 'origin/main' into dev).
Scope: Build-time dependencies such as
docs/follow the process above. Cargo production dependencies (runtime dependencies included in the binary) are not covered: waiting for the weekly version update or the next release would delay the fix by more than a week. In that case, manually open a PR ondev, which will reachmainin the next release.
Release Workflow
graph TD
A[dev 分支] --> B[创建 release 分支]
B --> C[版本准备]
C --> D[测试验证]
D --> E[创建 PR 到 main]
E --> F[最终审查]
F --> G[合并到 main]
G --> H[打版本标签]
H --> I[合并回 dev]
J[清理 release 分支]🗂️ Worktree Parallel Workbench
Applicable scenario: One person needs to handle multiple tasks that "require a branch" in parallel while keeping the
devworking directory stable. By convention, one worktree serves as a personal parallel workbench—create it as needed and delete it when finished; do not keep it permanently. An idle workbench only consumes disk space (this machine once had a persistent workbench atE:/git/YaoXiang-wt, which was deleted on 2026-10-15 because it was idle and had no unmerged commits).
Conventions
| Item | Value | Description |
|---|---|---|
| Workbench Path | E:/git/YaoXiang-wt | Fixed path with no task suffix; use git worktree move to rename it |
| Parking Branch | scratch | A "parking spot" between tasks; exists only locally, is never pushed, and is deleted with the workbench |
| Task Branch | Follow the naming rules above | Each task branch is created and used directly in the worktree and deleted when the task is complete |
Division of responsibility with the team orchestration convention in AGENTS.md for "validation evidence": It constrains the batch validation tree (with the single entry point
scripts/dev/scratch.py, always created under the repository working area attarget/yx-scratch/<batch>/tree, which is excluded by.gitignore(not committed; its contents must not be used as documentation citation sources—AGENTS meta-rule ①), and forbids members from creating directories outside the repository); this section describes a human parallel personal branch workbench. The former serves frozen-point measurement, while the latter serves branch development. Their purposes differ, but they follow the same principle: do not keep them around.
Complete Loop for a Task
# 0. Create as needed (delete when finished)
git worktree add E:/git/YaoXiang-wt -b scratch dev
# 1. Pick up the task: create the task branch from the latest dev in the worktree
cd E:/git/YaoXiang-wt
git switch -c <task-branch> dev
# 2. Develop and commit (the hook chain still runs normally in the worktree)
# 3. Merge: do this in the [MAIN REPOSITORY], not in the worktree
cd E:/git/YaoXiang
git merge <task-branch> # Fast-forward small fixes directly to keep dev's first-parent history linear
# 4. Wrap up: switch back to scratch → delete the merged task branch → delete the workbench
cd E:/git/YaoXiang-wt
git switch scratch
cd E:/git/YaoXiang
git branch -d <task-branch>
git worktree remove E:/git/YaoXiang-wt
git branch -d scratchThis loop is intended for quick personal fixes; larger changes should still follow the PR process above.
Hard Constraints and Pitfalls
- The same branch cannot be checked out by two worktrees at the same time. Since
devresides in the main repository,git switch devis rejected in the worktree, so you can only create a new branch based ondev—this is precisely why thescratchparking branch is needed. - Always merge in the main repository. The main repository holds
devand is the only merge point; the worktree is responsible only for development and commits on the task branch. - Build artifacts under
target/are not shared. Each worktree compiles independently (this repository contains about 160,000 lines of Rust, and the first full recompilation takes several minutes). Conversely, the build cache remains useful only during periods when you work on branches for several consecutive days, which is its primary benefit. When idle, its benefit is zero, so it should be deleted promptly. - Hooks are shared and execute normally. A worktree shares the same hook configuration as the main repository, and the complete hook chain runs normally during commits (the historical worktree
GIT_DIRleakage issue has been fixed); do not runpre-commit install/autoupdatein the worktree. - Always use absolute paths. Relative paths can cause problems on Windows.
- Common management commands:
git worktree list/git worktree move <旧路径> <新路径>/git worktree remove <路径>. git worktree removemay leave directory remnants on Windows: After deleting a workbench in whichnode_moduleshad been installed (pnpm uses junctions), the files have been emptied and the worktree has been unregistered from.git/worktrees, but the junction skeleton and empty directories remain (one measured instance: 1238 junctions, 0 files, 0 bytes). At this point, the path is an ordinary directory and can be removed withcmd /c rd /s /q <路径>. Before deleting it, first confirm that the targets of all junctions are within that directory (Get-ChildItem -Recurse -Forceto retrieve each.Target); otherwise, recursive deletion may traverse into another tree.
🛡️ Branch Protection Strategy
Main Branch Protection
main branch
- Direct pushes are prohibited
- Merges must go through a PR
- Force pushes are prohibited
- Code review is required
- Status checks must pass
dev branch
- Direct pushes are prohibited for developers
- Merges must go through a PR
- Status checks must pass
- Administrators may push directly
Branch Permission Settings
| Branch Type | Developer | Maintainer | Administrator |
|---|---|---|---|
main | PR only | PR only | Approve PRs |
dev | Merge via PR | Merge via PR | Direct push |
feature/* | Full permissions | Full permissions | Full permissions |
hotfix/* | Full permissions | Full permissions | Full permissions |
✅ Best Practices
1. Branch Management
- Frequent synchronization: Regularly obtain the latest code from the
devbranch - Atomic commits: Each commit should contain only related changes
- Prompt cleanup: Delete completed feature branches promptly after merging
- Clear descriptions: Branch names and commit messages should clearly communicate their intent
2. Commit Convention
Follow the Commit Convention:
# Format
:emoji: type(scope): 主题(中文)
# Examples
:sparkles: feat(frontend): 添加类型推断功能
:bug: fix(parser): 修复解析器崩溃问题
:recycle: refactor(vm): 重构虚拟机内存管理3. Pull Requests
- Clear descriptions: Explain the changes and their reasons in detail
- Link issues: Use
Closes #123to link the relevant Issue - Prompt responses: Respond to review comments in a timely manner
- Sufficient testing: Ensure that all tests pass
4. Code Review
- Functional correctness: Verify that the code works correctly
- Code quality: Check that the code follows the conventions
- Test coverage: Ensure that appropriate tests are included
- Documentation updates: Check whether the documentation needs to be updated
❓ Frequently Asked Questions
Q1: How do I choose a branch type?
Answer:
- New feature →
feature/ - Known defect fix →
bugfix/ - Urgent production fix →
hotfix/ - Documentation update →
docs/ - Code refactoring →
refactor/ - Test-related work →
test/
Q2: From which branch should a feature branch be created?
Answer: Always create it from the dev branch to ensure that the feature is based on the latest development code:
git checkout dev
git pull origin dev
git checkout -b feature/new-featureQ3: When should a release branch be created?
Answer:
- When preparing to release a new version
- When new features need to be frozen
- When a stable version needs dedicated testing
Q4: How should branch conflicts be handled?
Answer:
- Update the target branch:
git checkout dev && git pull origin dev - Switch to the feature branch:
git checkout feature/your-branch - Merge and resolve conflicts:
git rebase devorgit merge dev - Continue development after resolving the conflicts
Taking one side of an entire file is prohibited during conflict resolution: You must not use git checkout --ours / --theirs (or an equivalent method) to take one side of an entire file—this would silently discard all changes from the other side. The correct approach is a three-way merge with git merge-file or resolution hunk by hunk. After resolving the conflicts, you must compile locally and run tests for the affected areas before committing. Commits that claim to be a "pure move" are subject to the same constraint—the criterion for a correct move is "zero semantic diff except for the path."
Q5: How should a hotfix branch be handled?
Answer:
- Create it from the
mainbranch:git checkout main && git checkout -b hotfix/urgent-fix - Fix the issue and test the fix
- Create PRs to both
mainanddev - Deploy immediately after merging
Q6: Is there a length limit for branch names?
Answer: It is recommended that branch names not exceed 50 characters to keep them concise and clear. Git itself supports longer names, but overly long names reduce readability.
📚 Related Documentation
🔧 Tools and Scripts
Bulk Cleanup of Merged Branches
# Delete local branches already merged into dev
git checkout dev
git pull origin dev
git branch --merged dev | grep -E "^(feature|bugfix|docs|refactor|test)/" | xargs -n 1 git branch -d
# Delete remote branches that have already been merged
git remote prune originBranch Creation Template
#!/bin/bash
# Helper script for creating a feature branch
BRANCH_TYPE=$1
BRANCH_NAME=$2
if [ -z "$BRANCH_TYPE" ] || [ -z "$BRANCH_NAME" ]; then
echo "用法: $0 <类型> <分支名>"
echo "类型: feature, bugfix, hotfix, docs, refactor, test"
exit 1
fi
git checkout dev
git pull origin dev
git checkout -b "$BRANCH_TYPE/$BRANCH_NAME"
git push -u origin "$BRANCH_TYPE/$BRANCH_NAME"
echo "已创建并推送分支: $BRANCH_TYPE/$BRANCH_NAME"💡 Tip: Keep branches atomic and focused, with each branch doing only one thing. This makes code management clearer and more efficient.
📞 Support: If you have any questions, please discuss them in GitHub Discussions.
