Skip to content

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 ​

Core Branches ​

Branch NamePurposeLifecycleProtection Level
mainProduction codePermanentStrict protection
devPrimary development branchPermanentModerate protection

Feature Branches ​

PrefixPurposeNaming ExamplesMerge Target
feature/New feature developmentfeature/type-inference
feature/ownership-model
dev
bugfix/Fixing known defectsbugfix/memory-leak
bugfix/parser-error
dev
hotfix/Urgent production fixeshotfix/security-patch
hotfix/crash-bug
main + dev
release/Release preparation branchesrelease/v0.8.0
release/v1.0.0
main

Auxiliary Branches ​

PrefixPurposeNaming ExamplesMerge Target
docs/Documentation updatesdocs/api-reference
docs/tutorial-update
dev
ci/CI/CD configuration changesci/add-deploy-script
ci/optimize-build
dev
refactor/Code refactoringrefactor/lexer-optimization
refactor/memory-manager
dev
test/Test-related changestest/add-integration
test/performance-bench
dev

📝 Naming Rules ​

Basic Naming Format ​

bash
# Feature branch
<type>/<short-description>

# Examples
feature/add-type-inference
bugfix/fix-parser-crash
hotfix/security-vulnerability

Naming Conventions ​

  1. Use lowercase letters: Use lowercase letters in all branch names
  2. Separate words with hyphens: Use - to separate words; do not use underscores
  3. Use descriptive names: Branch names should clearly communicate their purpose
  4. Avoid special characters: Do not use spaces, periods, or other special characters
  5. Length limit: Branch names must not exceed 50 characters

Detailed Examples ​

bash
# ✅ 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 ​

bash
# 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-name

Branch Development ​

bash
# 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-name

Branch Merging ​

bash
# 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 remotely

Branch Deletion ​

bash
# 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 ​

mermaid
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 ​

mermaid
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:

  1. Merge it into main. Do not change the base to dev—alert determination is based on the default branch, and changing the base only leaves the alert open; Dependabot may also open another PR against main for the same alert.
  2. On the same day, merge main back into dev:
bash
git fetch origin
git switch dev
git merge --ff-only origin/main   # Fast-forward directly when dev has no independent commits
git push origin dev

If 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 on dev, which will reach main in the next release.

Release Workflow ​

mermaid
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 dev working 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 at E:/git/YaoXiang-wt, which was deleted on 2026-10-15 because it was idle and had no unmerged commits).

Conventions ​

ItemValueDescription
Workbench PathE:/git/YaoXiang-wtFixed path with no task suffix; use git worktree move to rename it
Parking BranchscratchA "parking spot" between tasks; exists only locally, is never pushed, and is deleted with the workbench
Task BranchFollow the naming rules aboveEach 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 at target/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 ​

bash
# 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 scratch

This loop is intended for quick personal fixes; larger changes should still follow the PR process above.

Hard Constraints and Pitfalls ​

  1. The same branch cannot be checked out by two worktrees at the same time. Since dev resides in the main repository, git switch dev is rejected in the worktree, so you can only create a new branch based on dev—this is precisely why the scratch parking branch is needed.
  2. Always merge in the main repository. The main repository holds dev and is the only merge point; the worktree is responsible only for development and commits on the task branch.
  3. 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.
  4. 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_DIR leakage issue has been fixed); do not run pre-commit install / autoupdate in the worktree.
  5. Always use absolute paths. Relative paths can cause problems on Windows.
  6. Common management commands: git worktree list / git worktree move <旧路径> <新路径> / git worktree remove <路径>.
  7. git worktree remove may leave directory remnants on Windows: After deleting a workbench in which node_modules had 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 with cmd /c rd /s /q <路径>. Before deleting it, first confirm that the targets of all junctions are within that directory (Get-ChildItem -Recurse -Force to 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 TypeDeveloperMaintainerAdministrator
mainPR onlyPR onlyApprove PRs
devMerge via PRMerge via PRDirect push
feature/*Full permissionsFull permissionsFull permissions
hotfix/*Full permissionsFull permissionsFull permissions

✅ Best Practices ​

1. Branch Management ​

  • Frequent synchronization: Regularly obtain the latest code from the dev branch
  • 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:

bash
# 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 #123 to 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:

bash
git checkout dev
git pull origin dev
git checkout -b feature/new-feature

Q3: 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:

  1. Update the target branch: git checkout dev && git pull origin dev
  2. Switch to the feature branch: git checkout feature/your-branch
  3. Merge and resolve conflicts: git rebase dev or git merge dev
  4. 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:

  1. Create it from the main branch: git checkout main && git checkout -b hotfix/urgent-fix
  2. Fix the issue and test the fix
  3. Create PRs to both main and dev
  4. 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.



🔧 Tools and Scripts ​

Bulk Cleanup of Merged Branches ​

bash
# 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 origin

Branch Creation Template ​

bash
#!/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.