git_checkout
Switch to a specified branch in a Git repository. Provide the repository path and branch name to change the active branch.
Instructions
Switches branches
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo_path | Yes | ||
| branch_name | Yes |
Implementation Reference
- The git_checkout function that executes the checkout logic: validates branch name, resolves the ref, and runs 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}'" - The GitCheckout Pydantic model defining the input schema for the git_checkout tool (repo_path and branch_name fields).
class GitCheckout(BaseModel): repo_path: str branch_name: str - src/git/src/mcp_server_git/server.py:106-106 (registration)The enum entry CHECKOUT = 'git_checkout' that registers the tool name.
CHECKOUT = "git_checkout" - src/git/src/mcp_server_git/server.py:408-418 (registration)The Tool registration object listing the git_checkout tool in the list_tools handler.
Tool( name=GitTools.CHECKOUT, description="Switches branches", inputSchema=GitCheckout.model_json_schema(), annotations=ToolAnnotations( readOnlyHint=False, destructiveHint=False, idempotentHint=False, openWorldHint=False, ), ), - src/git/src/mcp_server_git/server.py:556-561 (registration)The call_tool match case that dispatches to git_checkout when the tool name is CHECKOUT.
case GitTools.CHECKOUT: result = git_checkout(repo, arguments["branch_name"]) return [TextContent( type="text", text=result )]