create-branch
Initiate and manage a new git branch from a specified repository, with options to define a starting point, switch to the branch, or force creation if it exists.
Instructions
Create a new git branch, optionally from a specific starting point.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| branchName | Yes | Name of the new branch to create | |
| force | No | Force create branch, resetting it if it already exists (-B flag) | |
| repoPath | Yes | Absolute path to the git repository | |
| startPoint | No | Starting point for new branch (commit, branch, or tag). Defaults to current HEAD | |
| switchToBranch | No | Switch to the new branch after creation (default: true) |
Implementation Reference
- The private #handle method implements the core logic of the 'create-branch' tool, using simple-git to create a new branch with options for starting point, force, and checkout.readonly #handle: ToolCallback<typeof GIT_CREATE_BRANCH_INPUT_SCHEMA> = async (input) => { const sg = simpleGit(input.repoPath); const isRepo = await sg.checkIsRepo(); if (!isRepo) { return { isError: true, content: [ { type: 'text', text: 'Not a git repository', }, ], }; } const startPoint = input.startPoint ?? 'HEAD'; // Force create branch with switch (equivalent to git checkout -B) if (input.force && input.switchToBranch) { await sg.checkout(['-B', input.branchName, startPoint]); return { content: [ { type: 'text', text: `Force created and switched to branch '${input.branchName}' from '${startPoint}'`, }, ], }; } // Force create branch without switch if (input.force) { const result = await sg.branch(['-B', input.branchName, startPoint]); return { content: [ { type: 'text', text: `Force created branch '${input.branchName}' from '${startPoint}'`, }, { type: 'text', text: JSON.stringify(result), }, ], }; } // Regular branch creation with switch if (input.switchToBranch) { await (input.startPoint ? sg.checkoutBranch(input.branchName, input.startPoint) : sg.checkoutLocalBranch(input.branchName)); return { content: [ { type: 'text', text: `Created and switched to branch '${input.branchName}'${input.startPoint ? ` from '${input.startPoint}'` : ''}`, }, ], }; } // Regular branch creation without switch const result = await sg.branch([input.branchName, startPoint]); return { content: [ { type: 'text', text: `Created branch '${input.branchName}' from '${startPoint}'`, }, { type: 'text', text: JSON.stringify(result), }, ], }; };
- Zod schema defining the input parameters for the create-branch tool.export const GIT_CREATE_BRANCH_INPUT_SCHEMA = { repoPath: z.string().describe('Absolute path to the git repository'), branchName: z.string().describe('Name of the new branch to create'), startPoint: z .string() .optional() .describe('Starting point for new branch (commit, branch, or tag). Defaults to current HEAD'), switchToBranch: z .boolean() .optional() .default(true) .describe('Switch to the new branch after creation (default: true)'), force: z.boolean().optional().describe('Force create branch, resetting it if it already exists (-B flag)'), };
- packages/mcp-git/src/tools/create-branch.ts:40-42 (registration)The register method in GitCreateBranchTool class that registers the tool with the MCP server.register(srv: McpServer) { srv.registerTool(this.name, this.config, this.#handle); }
- packages/mcp-git/src/index.ts:24-24 (registration)Instantiation and registration call for the GitCreateBranchTool in the main server setup.new GitCreateBranchTool().register(server);
- Getter returning the tool name 'create-branch'.get name() { return 'create-branch'; }