Skip to main content
Glama
MementoRC

MCP Git Server

by MementoRC

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
NameRequiredDescriptionDefault
base_branchNo
branch_nameYes
repo_pathYes

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
  • 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"],
    ),
  • 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)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/MementoRC/mcp-git'

If you have feedback or need assistance with the MCP directory API, please join our Discord server