git_create_branch
Create a new branch in a Git repository, optionally specifying a base branch to branch from.
Instructions
Creates a new branch from an optional base branch
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo_path | Yes | ||
| branch_name | Yes | ||
| base_branch | No |
Implementation Reference
- Pydantic schema for git_create_branch input: repo_path, branch_name, optional base_branch
class GitCreateBranch(BaseModel): repo_path: str branch_name: str base_branch: str | None = None - src/git/src/mcp_server_git/server.py:105-106 (registration)Enum value mapping GitTools.CREATE_BRANCH to the string 'git_create_branch'
CREATE_BRANCH = "git_create_branch" CHECKOUT = "git_checkout" - src/git/src/mcp_server_git/server.py:397-407 (registration)Tool registration in list_tools() with schema, description, and annotations
Tool( name=GitTools.CREATE_BRANCH, description="Creates a new branch from an optional base branch", inputSchema=GitCreateBranch.model_json_schema(), annotations=ToolAnnotations( readOnlyHint=False, destructiveHint=False, idempotentHint=False, openWorldHint=False, ), ), - Core handler function that creates a branch. Validates names (rejects '-' prefix), resolves base branch or uses active branch, then calls repo.create_head()
def git_create_branch(repo: git.Repo, branch_name: str, base_branch: str | None = None) -> str: # Defense in depth: reject names starting with '-' to prevent flag injection if branch_name.startswith("-"): raise BadName(f"Invalid branch name: '{branch_name}' - cannot start with '-'") if base_branch and base_branch.startswith("-"): raise BadName(f"Invalid base branch: '{base_branch}' - cannot start with '-'") if base_branch: base = repo.references[base_branch] else: base = repo.active_branch repo.create_head(branch_name, base) return f"Created branch '{branch_name}' from '{base.name}'" - src/git/src/mcp_server_git/server.py:545-554 (registration)Handler dispatch in call_tool() that routes GitTools.CREATE_BRANCH to the git_create_branch function
case GitTools.CREATE_BRANCH: result = git_create_branch( repo, arguments["branch_name"], arguments.get("base_branch") ) return [TextContent( type="text", text=result )]