Skip to main content
Glama
piyushgIITian

GitHub Enterprise MCP Server

create-branch

Create a new branch in a GitHub repository by specifying the owner, repository name, and branch name, with optional source branch selection.

Instructions

Create a new branch in a GitHub repository

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
branchYesName for the new branch
from_branchNoSource branch to create from (defaults to the repository's default branch)
ownerYesRepository owner
repoYesRepository name

Implementation Reference

  • The core handler function that executes the create-branch tool. Validates inputs using CreateBranchSchema, checks if the branch exists, determines the source branch, fetches its SHA, and creates the new branch ref via GitHub API.
    export async function createBranch(args: unknown): Promise<any> { const { owner, repo, branch, from_branch } = CreateBranchSchema.parse(args); const github = getGitHubApi(); return tryCatchAsync(async () => { // Check if branch already exists try { await github.getOctokit().git.getRef({ owner, repo, ref: `heads/${branch}`, }); return { success: false, message: `Branch '${branch}' already exists in ${owner}/${repo}`, }; } catch (error: any) { // If error is not 404 (not found), rethrow it if (error.status !== 404) { throw error; } } // Get the SHA of the latest commit on the source branch const sourceBranch = from_branch || await github.getDefaultBranch(owner, repo); const { data: refData } = await github.getOctokit().git.getRef({ owner, repo, ref: `heads/${sourceBranch}`, }); // Create a new branch from the source branch const { data } = await github.getOctokit().git.createRef({ owner, repo, ref: `refs/heads/${branch}`, sha: refData.object.sha, }); return { success: true, ref: data.ref, url: data.url, object: { sha: data.object.sha, type: data.object.type, url: data.object.url, }, message: `Branch '${branch}' created from '${sourceBranch}' in ${owner}/${repo}`, }; }, 'Failed to create branch'); }
  • Zod validation schema used in the handler to parse and validate the input arguments for create-branch.
    export const CreateBranchSchema = OwnerRepoSchema.extend({ branch: z.string().min(1, 'Branch name is required'), from_branch: z.string().optional(), });
  • Switch case in the MCP tool call handler that routes 'create-branch' invocations to the createBranch function.
    case 'create-branch': result = await createBranch(parsedArgs); break;
  • Input schema definition for the create-branch tool in the MCP server's listTools response.
    { name: 'create-branch', description: 'Create a new branch in a GitHub repository', inputSchema: { type: 'object', properties: { owner: { type: 'string', description: 'Repository owner', }, repo: { type: 'string', description: 'Repository name', }, branch: { type: 'string', description: 'Name for the new branch', }, from_branch: { type: 'string', description: 'Source branch to create from (defaults to the repository\'s default branch)', }, }, required: ['owner', 'repo', 'branch'], additionalProperties: false, }, },

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/piyushgIITian/github-enterprice-mcp'

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