branch_create
Create a new branch in a specified Git repository using the Git MCP Server. Input branch name and repository path; optionally force creation, set tracking, or configure upstream for push/pull operations.
Instructions
Create a new branch
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| force | No | Force create branch even if it exists | |
| name | Yes | Branch name | |
| path | No | Path to repository. MUST be an absolute path (e.g., /Users/username/projects/my-repo) | |
| setUpstream | No | Set upstream for push/pull | |
| track | No | Set up tracking mode |
Input Schema (JSON Schema)
{
"properties": {
"force": {
"default": false,
"description": "Force create branch even if it exists",
"type": "boolean"
},
"name": {
"description": "Branch name",
"type": "string"
},
"path": {
"description": "Path to repository. MUST be an absolute path (e.g., /Users/username/projects/my-repo)",
"type": "string"
},
"setUpstream": {
"default": false,
"description": "Set upstream for push/pull",
"type": "boolean"
},
"track": {
"default": true,
"description": "Set up tracking mode",
"type": "boolean"
}
},
"required": [
"name"
],
"type": "object"
}
Implementation Reference
- src/git-operations.ts:402-430 (handler)The primary handler function for 'branch_create' tool. Validates repository and branch name, executes 'git checkout -b' command with flags for force, track, and setUpstream, handles caching invalidation for branch state, and returns formatted success message.static async branchCreate({ path, name, force, track, setUpstream }: BranchOptions, context: GitToolContext): Promise<GitToolResult> { const resolvedPath = this.getPath({ path }); return await this.executeOperation( context.operation, resolvedPath, async () => { const { path: repoPath } = PathValidator.validateGitRepo(resolvedPath); PathValidator.validateBranchName(name); const result = await CommandExecutor.executeGitCommand( `checkout -b ${name}${force ? ' --force' : ''}${track ? ' --track' : ' --no-track'}${setUpstream ? ' --set-upstream' : ''}`, context.operation, repoPath ); return { content: [{ type: 'text', text: `Branch '${name}' created successfully\n${CommandExecutor.formatOutput(result)}` }] }; }, { command: 'branch_create', invalidateCache: true, // Invalidate branch cache stateType: RepoStateType.BRANCH } ); }
- src/tool-handler.ts:231-262 (registration)MCP tool registration for 'branch_create', defining the tool name, description, input schema with properties for path, name, force, track, setUpstream, and required fields.{ name: 'branch_create', description: 'Create a new branch', inputSchema: { type: 'object', properties: { path: { type: 'string', description: `Path to repository. ${PATH_DESCRIPTION}`, }, name: { type: 'string', description: 'Branch name', }, force: { type: 'boolean', description: 'Force create branch even if it exists', default: false }, track: { type: 'boolean', description: 'Set up tracking mode', default: true }, setUpstream: { type: 'boolean', description: 'Set upstream for push/pull', default: false } }, required: ['name'], },
- src/tool-handler.ts:608-611 (schema)Tool dispatch handler in switch statement that validates arguments using isBranchOptions type guard and calls GitOperations.branchCreate.case 'branch_create': { const validArgs = this.validateArguments(operation, args, isBranchOptions); return await GitOperations.branchCreate(validArgs, context); }