Skip to main content
Glama

branch_delete

Delete a specific branch in a Git repository by specifying the branch name and repository path. Enables precise branch management through the Git MCP Server for enhanced Git operations.

Instructions

Delete a branch

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
nameYesBranch name
pathNoPath to repository. MUST be an absolute path (e.g., /Users/username/projects/my-repo)

Implementation Reference

  • Primary implementation of the branch_delete tool handler. Validates repository and branch existence, ensures it's not the current branch, executes 'git branch -D <name>', handles errors, manages caching, and returns formatted success message.
    static async branchDelete({ path, name }: 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);
          await RepositoryValidator.validateBranchExists(repoPath, name, context.operation);
          
          const currentBranch = await RepositoryValidator.getCurrentBranch(repoPath, context.operation);
          if (currentBranch === name) {
            throw ErrorHandler.handleValidationError(
              new Error(`Cannot delete the currently checked out branch: ${name}`),
              { operation: context.operation, path: repoPath }
            );
          }
          
          const result = await CommandExecutor.executeGitCommand(
            `branch -D ${name}`,
            context.operation,
            repoPath
          );
    
          return {
            content: [{
              type: 'text',
              text: `Branch '${name}' deleted successfully\n${CommandExecutor.formatOutput(result)}`
            }]
          };
        },
        {
          command: 'branch_delete',
          invalidateCache: true, // Invalidate branch cache
          stateType: RepoStateType.BRANCH
        }
      );
    }
  • Registers the 'branch_delete' tool with the MCP server, defining its name, description, and input schema (path and name parameters).
    {
      name: 'branch_delete',
      description: 'Delete a branch',
      inputSchema: {
        type: 'object',
        properties: {
          path: {
            type: 'string',
            description: `Path to repository. ${PATH_DESCRIPTION}`,
          },
          name: {
            type: 'string',
            description: 'Branch name',
          },
        },
        required: ['name'],
      },
    },
  • Dispatch handler in the tool executor switch statement that validates arguments using isBranchOptions and calls the main GitOperations.branchDelete handler.
    case 'branch_delete': {
      const validArgs = this.validateArguments(operation, args, isBranchOptions);
      return await GitOperations.branchDelete(validArgs, context);
    }
  • Type definition for BranchDeleteOptions interface used in branch operations (though simplified in tool usage).
    export interface BranchDeleteOptions extends GitOperationOptions {
      /** Name of the branch to delete */
      name: string;
      /** Whether to force delete even if not merged */
      force?: boolean;
      /** Also delete the branch from remotes */
      remote?: boolean;
    }
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/Sheshiyer/git-mcp-v2'

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