create_branch
Initiate a new branch in a GitHub repository by specifying the owner, repo, and branch name, optionally basing it on an existing branch.
Instructions
Create a new branch in a GitHub repository
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| branch | Yes | Name for the new branch | |
| from_branch | No | Optional: source branch to create from (defaults to the repository's default branch) | |
| owner | Yes | Repository owner (username or organization) | |
| repo | Yes | Repository name |
Implementation Reference
- src/operations/branches.ts:85-103 (handler)Primary handler function for the 'create_branch' tool. Determines the SHA from the source branch or default branch, then creates the new branch using the core createBranch function.export async function createBranchFromRef( github_pat: string, owner: string, repo: string, newBranch: string, fromBranch?: string ): Promise<z.infer<typeof GitHubReferenceSchema>> { let sha: string; if (fromBranch) { sha = await getBranchSHA(github_pat, owner, repo, fromBranch); } else { sha = await getDefaultBranchSHA(github_pat, owner, repo); } return createBranch(github_pat, owner, repo, { ref: newBranch, sha, }); }
- src/operations/branches.ts:47-68 (helper)Core helper function that performs the actual GitHub API POST request to create the branch reference.export async function createBranch( github_pat: string, owner: string, repo: string, options: CreateBranchOptions ): Promise<z.infer<typeof GitHubReferenceSchema>> { const fullRef = `refs/heads/${options.ref}`; const response = await githubRequest( github_pat, `https://api.github.com/repos/${owner}/${repo}/git/refs`, { method: "POST", body: { ref: fullRef, sha: options.sha, }, } ); return GitHubReferenceSchema.parse(response); }
- src/operations/branches.ts:11-20 (schema)Schema definitions: CreateBranchSchema (public input schema) and _CreateBranchSchema (internal with github_pat) used for validation in the tool handler.export const CreateBranchSchema = z.object({ owner: z.string().describe("Repository owner (username or organization)"), repo: z.string().describe("Repository name"), branch: z.string().describe("Name for the new branch"), from_branch: z.string().optional().describe("Optional: source branch to create from (defaults to the repository's default branch)"), }); export const _CreateBranchSchema = CreateBranchSchema.extend({ github_pat: z.string().describe("GitHub Personal Access Token"), });
- src/index.ts:119-122 (registration)Tool registration in the listTools response, defining name, description, and input schema.name: "create_branch", description: "Create a new branch in a GitHub repository", inputSchema: zodToJsonSchema(branches.CreateBranchSchema), },
- src/index.ts:339-351 (handler)Top-level dispatch handler in the main CallToolRequest handler that parses arguments and invokes the branches.createBranchFromRef implementation.case "create_branch": { const args = branches._CreateBranchSchema.parse(params.arguments); const branch = await branches.createBranchFromRef( args.github_pat, args.owner, args.repo, args.branch, args.from_branch ); return { content: [{ type: "text", text: JSON.stringify(branch, null, 2) }], }; }