返回技能列表

manage-git-branches

pjt222
更新于 2 days ago
6 次查看
17
2
17
在 GitHub 上查看
ai

关于

This Claude skill helps developers manage Git branches for creating features, switching contexts, and keeping branches synchronized. It covers branch naming, safe switching with stash, upstream sync, and cleanup of merged branches. Use it when starting new work, switching tasks, or maintaining branch hygiene after merges.

快速安装

Claude Code

推荐
主要方式
npx skills add pjt222/agent-almanac -a claude-code
插件命令备选方式
/plugin add https://github.com/pjt222/agent-almanac
Git 克隆备选方式
git clone https://github.com/pjt222/agent-almanac.git ~/.claude/skills/manage-git-branches

在 Claude Code 中复制并粘贴此命令以安装该技能

技能文档

Manage Git Branches

Create, switch, sync, and clean up branches following consistent naming conventions.

When to Use

  • Starting work on a new feature or bug fix
  • Switching between tasks on different branches
  • Keeping a feature branch up to date with main
  • Cleaning up branches after merging pull requests
  • Listing and inspecting branches

Inputs

  • Required: Repository with at least one commit
  • Optional: Branch naming convention (default: type/description)
  • Optional: Base branch for new branches (default: main)
  • Optional: Remote name (default: origin)

Procedure

Step 1: Create a Feature Branch

Use a consistent naming convention:

PrefixPurposeExample
feature/New functionalityfeature/add-weighted-mean
fix/Bug fixfix/null-pointer-in-parser
docs/Documentationdocs/update-api-reference
refactor/Code restructuringrefactor/extract-validation
chore/Maintenancechore/update-dependencies
test/Test additionstest/add-edge-case-coverage
# Create and switch to a new branch from main
git checkout -b feature/add-weighted-mean main

# Or using the newer switch command
git switch -c feature/add-weighted-mean main

Got: New branch created and checked out. git branch shows the new branch with an asterisk.

If fail: If the base branch doesn't exist locally, fetch first: git fetch origin main && git checkout -b feature/name origin/main.

Step 2: Track Remote Branches

Set up tracking when pushing a new branch for the first time:

# Push and set upstream tracking
git push -u origin feature/add-weighted-mean

# Check tracking relationship
git branch -vv

To check out a remote branch that someone else created:

git fetch origin
git checkout feature/their-branch
# Git auto-creates a local tracking branch

Got: Local branch tracks the corresponding remote branch. git branch -vv shows the upstream.

If fail: If auto-tracking fails, set it manually: git branch --set-upstream-to=origin/feature/name feature/name.

Step 3: Switch Branches Safely

Before switching, ensure the working tree is clean:

# Check for uncommitted changes
git status

If changes exist, either commit or stash them:

# Option 1: Commit work in progress
git add <files>
git commit -m "wip: save progress on validation logic"

# Option 2: Stash changes temporarily
git stash push -m "validation work in progress"

# Switch branches
git checkout main

# Later, restore stashed changes
git checkout feature/add-weighted-mean
git stash pop

List and manage stashes:

# List all stashes
git stash list

# Apply a specific stash (without removing it)
git stash apply stash@{1}

# Drop a stash
git stash drop stash@{0}

Got: Branch switch succeeds. Working tree reflects the target branch's state. Stashed changes are recoverable.

If fail: If switch is blocked by uncommitted changes that would be overwritten, stash or commit first. git stash cannot stash untracked files unless you use git stash push -u.

Step 4: Sync with Upstream

Keep your feature branch up to date with the base branch:

# Fetch latest changes
git fetch origin

# Rebase onto latest main (preferred — keeps linear history)
git rebase origin/main

# Or merge main into your branch (creates merge commit)
git merge origin/main

Got: Branch now includes the latest changes from main. No conflicts, or conflicts resolved (see resolve-git-conflicts).

If fail: If rebase causes conflicts, resolve each one and git rebase --continue. If the conflicts are too complex, abort with git rebase --abort and try git merge origin/main instead.

Step 5: Clean Up Merged Branches

After pull requests are merged, remove stale branches:

# Delete a local branch that has been merged
git branch -d feature/add-weighted-mean

# Delete a local branch (force, even if not merged)
git branch -D feature/abandoned-experiment

# Delete a remote branch
git push origin --delete feature/add-weighted-mean

# Prune remote-tracking references for deleted remote branches
git fetch --prune

Got: Merged branches are removed locally and remotely. git branch shows only active branches.

If fail: git branch -d refuses to delete unmerged branches. If the branch was merged via squash merge on GitHub, Git may not recognize it as merged. Use git branch -D if you are certain the work is preserved.

Step 6: List and Inspect Branches

# List local branches
git branch

# List all branches (local and remote)
git branch -a

# List branches with last commit info
git branch -v

# List branches merged into main
git branch --merged main

# List branches NOT yet merged
git branch --no-merged main

# See which remote branch each local branch tracks
git branch -vv

Got: Clear view of all branches, their status, and tracking relationships.

If fail: If remote branches appear stale, run git fetch --prune to clean up references to deleted remote branches.

Validation

  • Branch names follow the agreed naming convention
  • Feature branches are created from the correct base branch
  • Local branches track their remote counterparts
  • Merged branches are cleaned up (local and remote)
  • Working tree is clean before branch switches
  • Stashed changes are not left orphaned

Pitfalls

  • Working on main directly: Always create a feature branch. Committing directly to main makes it difficult to create PRs and collaborate.
  • Forgetting to fetch before branching: Creating a branch from a stale local main means you start behind. Always git fetch origin first.
  • Long-lived branches: Feature branches that live for weeks accumulate merge conflicts. Sync frequently and keep branches short-lived.
  • Orphaned stashes: git stash is temporary storage. Don't rely on it for long-term work. Commit or branch instead.
  • Deleting unmerged work: git branch -D is destructive. Double-check with git log branch-name before force-deleting.
  • Not pruning: Remote branches deleted on GitHub still appear locally until you git fetch --prune.

Related Skills

  • commit-changes - committing work on branches
  • create-pull-request - opening PRs from feature branches
  • resolve-git-conflicts - handling conflicts during sync
  • configure-git-repository - repository setup and branch strategy

GitHub 仓库

pjt222/agent-almanac
路径: i18n/caveman-lite/skills/manage-git-branches
0
agentsagentskillsai-assisted-developmentclaude-codeskillsteams

相关推荐技能

content-collections

Content Collections 是一个 TypeScript 优先的构建工具,可将本地 Markdown/MDX 文件转换为类型安全的数据集合。它专为构建博客、文档站和内容密集型 Vite+React 应用而设计,提供基于 Zod 的自动模式验证。该工具涵盖从 Vite 插件配置、MDX 编译到生产环境部署的完整工作流。

查看技能

polymarket

这个Claude Skill为开发者提供完整的Polymarket预测市场开发支持,涵盖API调用、交易执行和市场数据分析。关键特性包括实时WebSocket数据流,可监控实时交易、订单和市场动态。开发者可用它构建预测市场应用、实施交易策略并集成实时市场预测功能。

查看技能

creating-opencode-plugins

该Skill帮助开发者创建OpenCode插件,用于接入命令、文件、LSP等25+种事件。它提供了插件结构、事件API规范和JavaScript/TypeScript实现模式,适合需要拦截操作、扩展功能或自定义事件处理的场景。开发者可通过它快速构建响应式模块来增强OpenCode AI助手的能力。

查看技能

sglang

SGLang是一个专为LLM设计的高性能推理框架,特别适用于需要结构化输出的场景。它通过RadixAttention前缀缓存技术,在处理JSON、正则表达式、工具调用等具有重复前缀的复杂工作流时,能实现极速生成。如果你正在构建智能体或多轮对话系统,并追求远超vLLM的推理性能,SGLang是理想选择。

查看技能