git_create_branch
Create a new branch in a Git repository, optionally specifying a base branch to start from, using the MCP Git Server's branch management tool.
Instructions
Creates a new branch from an optional base branch
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| base_branch | No | ||
| branch_name | Yes | ||
| repo_path | Yes |
Implementation Reference
- Core handler function that implements the git_create_branch tool logic using GitPython Repo.create_head() to create a new branch from an optional base branch, with existence checks and error handling.def git_create_branch( repo: Repo, branch_name: str, base_branch: str | None = None, start_point: str | None = None, ) -> str: """Create new branch from base - UPDATED VERSION 2024""" try: # INTEGRATED DEBUG LOGGING # Use start_point if provided, otherwise fall back to base_branch effective_base = start_point if start_point is not None else base_branch # Check if branch already exists existing_branches = [branch.name for branch in repo.branches] if branch_name in existing_branches: return f"❌ Branch '{branch_name}' already exists" # Create new branch if effective_base: # Verify base branch exists if effective_base not in existing_branches and effective_base not in [ branch.name for branch in repo.remote().refs ]: return f"❌ Base branch '{effective_base}' not found" repo.create_head(branch_name, effective_base) else: repo.create_head(branch_name) return f"✅ Created branch '{branch_name}'" except GitCommandError as e: return f"❌ Branch creation failed: {str(e)}" except Exception as e: return f"❌ Branch creation error: {str(e)}"
- Pydantic schema/model for input validation of git_create_branch tool parameters.class GitCreateBranch(BaseModel): repo_path: str branch_name: str base_branch: str | None = None
- src/mcp_server_git/core/handlers.py:96-100 (registration)Registration of the git_create_branch handler in the GitToolRouter via _get_git_handlers() method, wrapping the operations function with error handling and repo preparation."git_create_branch": self._create_git_handler( git_create_branch, requires_repo=True, extra_args=["branch_name", "base_branch"], ),
- src/mcp_server_git/core/tools.py:238-245 (registration)ToolDefinition registration in ToolRegistry for git_create_branch, including schema reference and metadata; handler later overridden by router.ToolDefinition( name=GitTools.CREATE_BRANCH, category=ToolCategory.GIT, description="Create a new branch from an optional base branch", schema=GitCreateBranch, handler=placeholder_handler, requires_repo=True, ),
- Protected wrapper for git_create_branch that adds repository binding validation and remote integrity checks before calling the core handler.async def protected_git_create_branch( self, repo_path: str, branch_name: str, base_branch: str | None = None ) -> str: """Git create branch with repository binding protection.""" validated_path = await self._validate_and_prepare_operation(repo_path) repo = Repo(validated_path) return git_create_branch(repo, branch_name, base_branch)