git_checkout
Switch between Git branches in a repository to manage development workflows and isolate changes.
Instructions
Switches branches
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo_path | Yes | ||
| branch_name | Yes |
Implementation Reference
- The main handler function that executes the git checkout by validating the branch name and calling repo.git.checkout.def git_checkout(repo: git.Repo, branch_name: str) -> str: # Defense in depth: reject branch names starting with '-' to prevent flag injection, # even if a malicious ref with that name exists (e.g. via filesystem manipulation) if branch_name.startswith("-"): raise BadName(f"Invalid branch name: '{branch_name}' - cannot start with '-'") repo.rev_parse(branch_name) # Validates branch_name is a real git ref, throws BadName if not repo.git.checkout(branch_name) return f"Switched to branch '{branch_name}'"
- Pydantic BaseModel defining the input schema for the git_checkout tool: repo_path and branch_name.class GitCheckout(BaseModel): repo_path: str branch_name: str
- src/git/src/mcp_server_git/server.py:332-336 (registration)Registration of the 'git_checkout' tool in the list_tools() function using name=GitTools.CHECKOUT and the GitCheckout schema.Tool( name=GitTools.CHECKOUT, description="Switches branches", inputSchema=GitCheckout.model_json_schema(), ),
- src/git/src/mcp_server_git/server.py:105-105 (registration)Enum definition mapping GitTools.CHECKOUT to the tool name string 'git_checkout'.CHECKOUT = "git_checkout"
- Dispatch in call_tool() that invokes the git_checkout handler and returns the result as TextContent.case GitTools.CHECKOUT: result = git_checkout(repo, arguments["branch_name"]) return [TextContent( type="text", text=result )]