# git branch

> Automated Git branch selection and repository state validation to prevent upstream conflicts and history rewrites.

git branch is a free AI agent skill on Agensi by David Tsao, in DevOps & Cloud. It loads into your AI agent through the Agensi MCP server, always in its latest approved version.

- Price: Free
- Creator: [David Tsao](https://www.agensi.io/creators/david-tsao)
- Category: [DevOps & Cloud](https://www.agensi.io/skills/devops-cloud)
- Uses: 2
- Version: 1.0
- Last updated: 2026-10-18
- Security: scan passed (2026-10-18); every new listing is also reviewed by a person
- Listing: https://www.agensi.io/skills/git-branch
- Tags: devops, git, github-cli, workflow-automation, code-quality-review

## About this skill

Developers often lose time rewriting history because they started coding on the wrong branch. This skill enforces a rigorous Git workflow by analyzing the repository state, PR status, and worktrees before the first line of code is written. It prevents common mistakes like committing to the default branch, adding feature code to unrelated fix branches, or working on squash-merged stubs.

### What it does
- **State validation** runs pruning fetches and checks for dirty trees or detached HEAD states.
- **Branch routing** determines if a task requires a new feature branch or an update to an unmerged PR.
- **Risk assessment** identifies squash-merged branches that appear active but are actually closed.
- **Naming enforcement** pulls the repo's default branch name and follows existing remote naming conventions.
- **Safety checks** prevents accidental stashing, rebasing, or force-pushing without explicit consent.

### How it works
1. **Sync and Inspect** triggers a prune fetch and inspects local branches, upstreams, and active worktrees.
2. **Context analysis** checks for existing PRs via GitHub CLI to see if the current task belongs to an open thread.
3. **Branch selection** creates a new branch from the correct base or stays on the current active feature branch.
4. **Resolution reporting** provides a concise summary of the chosen path and any mitigated risks.

### Frameworks & tools
This skill requires `git` and works best when `gh` (GitHub CLI) is available for PR status checks.

### Why this beats prompting it yourself
Manual branching is prone to "stale head" errors where you branch off an outdated main. This skill automates the fetch-and-check cycle, ensuring every task starts from the actual origin state while avoiding the trap of squash-merged local branches.

### Use cases
- Start a new feature off the correct, up-to-date default branch.
- Identify when a bug fix should be added to an existing unmerged PR.
- Prevent commits to branches that have already been squash-merged upstream.
- Navigate complex repos with multiple active worktrees and detached HEADs.

### Known limitations
Requires a local Git environment. Cannot perform stashes, rebases, or force-pushes without direct user confirmation.

## Requires

- Git

## How to use this skill

1. Connect your AI agent to Agensi once: add the MCP server https://mcp.agensi.io/mcp/auth and sign in with your Agensi account (OAuth, no API key). Steps for Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and other agents: https://www.agensi.io/docs/mcp-setup
2. Ask your agent: "Use the git branch skill from Agensi" and describe your task.
3. Your agent loads the skill from Agensi when it needs it. Nothing is downloaded.

## Frequently asked questions

### How does this skill prevent common Git workflow errors?

This skill automates the pre-coding workflow by verifying repository states, checking for open PRs, and ensuring you are on the correct branch, effectively preventing accidental commits to default branches or outdated codebases.

### Are there any specific software prerequisites for this skill to function?

The skill requires a local installation of `git` and highly recommends the GitHub CLI (`gh`) to enable advanced features like Pull Request status checks and remote branch validation.

### Can this skill detect if I am working on a branch that has already been merged?

Yes, it is designed to recognize squash-merged branches that may still appear active locally, preventing you from building on top of code that has already been integrated and deleted upstream.

### Does the skill support repositories that use Git worktrees?

Absolutely. The skill is designed to manage complex environments, including detached HEAD states and active worktrees, ensuring your task begins in the correct context regardless of your current setup.

### Will this skill automatically modify my local changes or force-push to remotes?

For security and data integrity, the skill will never perform destructive actions like stashing, rebasing, or force-pushing without your explicit manual confirmation.

---

Source: [Agensi](https://www.agensi.io), the marketplace of skills for AI agents. Any MCP-compatible agent (Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and more) connects at https://mcp.agensi.io/mcp/auth and signs in with an Agensi account; setup for every agent: https://www.agensi.io/docs/mcp-setup. Append .md to any skill, guide or docs address on agensi.io for its markdown version.
