create_branch
Create a new branch in a GitHub repository by specifying owner, repository name, and branch name, optionally from a source branch.
Instructions
Create a new branch in a GitHub repository
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| owner | Yes | Repository owner (username or organization) | |
| repo | Yes | Repository name | |
| branch | Yes | Name for the new branch | |
| from_branch | No | Optional: source branch to create from (defaults to the repository's default branch) |
Implementation Reference
- index.ts:177-188 (handler)Dispatcher handler for the "create_branch" tool: parses input arguments using the schema and calls the branches.createBranchFromRef function to execute the tool logic.case "create_branch": { const args = branches.CreateBranchSchema.parse(request.params.arguments); const branch = await branches.createBranchFromRef( args.owner, args.repo, args.branch, args.from_branch ); return { content: [{ type: "text", text: JSON.stringify(branch, null, 2) }], }; }
- operations/branches.ts:75-92 (handler)Primary handler function implementing the create_branch tool logic: determines the SHA of the source branch (or default), then calls createBranch to perform the GitHub API call.export async function createBranchFromRef( owner: string, repo: string, newBranch: string, fromBranch?: string ): Promise<z.infer<typeof GitHubReferenceSchema>> { let sha: string; if (fromBranch) { sha = await getBranchSHA(owner, repo, fromBranch); } else { sha = await getDefaultBranchSHA(owner, repo); } return createBranch(owner, repo, { ref: newBranch, sha, }); }
- operations/branches.ts:41-60 (helper)Core helper function that executes the GitHub API POST request to create a new git reference (branch).export async function createBranch( owner: string, repo: string, options: CreateBranchOptions ): Promise<z.infer<typeof GitHubReferenceSchema>> { const fullRef = `refs/heads/${options.ref}`; const response = await githubRequest( `https://api.github.com/repos/${owner}/${repo}/git/refs`, { method: "POST", body: { ref: fullRef, sha: options.sha, }, } ); return GitHubReferenceSchema.parse(response); }
- operations/branches.ts:11-16 (schema)Zod schema defining the input parameters for the create_branch tool, used for validation in the handler and tool registration."},{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)"), });