git_checkout
Switch between Git branches, restore files to specific versions, or create new branches to manage your code changes and repository state.
Instructions
Switch branches or restore files
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| target | Yes | Branch name, commit hash, or file path to checkout | |
| cwd | No | Repository directory | |
| createBranch | No | Create new branch | |
| force | No | Force checkout, discarding local changes |
Implementation Reference
- src/tools/git.ts:283-287 (handler)The main handler function that constructs and executes the 'git checkout' command using the shared executeGitCommand helper, handling branch creation and force flags.export async function gitCheckout(args: z.infer<typeof gitCheckoutSchema>): Promise<ToolResponse> { const createFlag = args.createBranch ? '-b' : ''; const forceFlag = args.force ? '-f' : ''; return executeGitCommand(`git checkout ${createFlag} ${forceFlag} ${args.target}`.trim(), args.cwd); }
- src/tools/git.ts:106-111 (schema)Zod schema defining the input parameters and validation for the git_checkout tool.export const gitCheckoutSchema = z.object({ target: z.string().describe('Branch name, commit hash, or file path to checkout'), cwd: z.string().optional().describe('Repository directory'), createBranch: z.boolean().optional().default(false).describe('Create new branch'), force: z.boolean().optional().default(false).describe('Force checkout, discarding local changes') });
- src/tools/git.ts:576-589 (registration)MCP tool registration entry in gitTools array, defining the tool name, description, and JSON input schema for protocol listing.{ name: 'git_checkout', description: 'Switch branches or restore files', inputSchema: { type: 'object', properties: { target: { type: 'string', description: 'Branch name, commit hash, or file path to checkout' }, cwd: { type: 'string', description: 'Repository directory' }, createBranch: { type: 'boolean', default: false, description: 'Create new branch' }, force: { type: 'boolean', default: false, description: 'Force checkout, discarding local changes' } }, required: ['target'] } },
- src/index.ts:381-384 (registration)Server request handler dispatcher that matches tool name 'git_checkout', validates arguments, and invokes the gitCheckout handler.if (name === 'git_checkout') { const validated = gitCheckoutSchema.parse(args); return await gitCheckout(validated); }
- src/tools/git.ts:21-61 (helper)Shared helper utility that executes git shell commands asynchronously, handles output/error formatting, and returns standardized ToolResponse.async function executeGitCommand(command: string, cwd?: string): Promise<ToolResponse> { try { const { stdout, stderr } = await execAsync(command, { cwd: cwd || process.cwd(), shell: '/bin/bash', maxBuffer: 10 * 1024 * 1024 // 10MB buffer }); return { content: [ { type: "text" as const, text: JSON.stringify({ success: true, command: command, stdout: stdout.trim(), stderr: stderr.trim(), cwd: cwd || process.cwd() }, null, 2) } ] }; } catch (error: any) { return { content: [ { type: "text" as const, text: JSON.stringify({ success: false, command: command, stdout: error.stdout?.trim() || '', stderr: error.stderr?.trim() || error.message, exitCode: error.code || 1, cwd: cwd || process.cwd() }, null, 2) } ], isError: true }; } }