Skip to main content
Glama

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
NameRequiredDescriptionDefault
branchNameYesName of the new branch to create
forceNoForce create branch, resetting it if it already exists (-B flag)
repoPathYesAbsolute path to the git repository
startPointNoStarting point for new branch (commit, branch, or tag). Defaults to current HEAD
switchToBranchNoSwitch 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)'),
    };
  • 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);
    }
  • 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';
    }
Install Server

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ver0-project/mcps'

If you have feedback or need assistance with the MCP directory API, please join our Discord server