git_commit_and_push
Automate Git workflows by committing staged changes and pushing to remote repositories. Specify files, branch, and commit messages with optional dry run previews for streamlined version control.
Instructions
Commit staged changes and push to remote repository
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| branch | No | Branch name to create/switch to (optional) | |
| commitMessage | Yes | Commit message (use conventional commit format) | |
| dryRun | No | Preview changes without executing | |
| files | No | Array of file paths to commit (empty for all changes) | |
| workingDir | No | Working directory path (defaults to current directory) |
Implementation Reference
- src/index.ts:144-231 (handler)The main handler function that performs the git commit and push operations: checks repo, handles branch, adds files, commits, pushes.async function commitAndPush( files: string[], commitMessage: string, branch?: string, workingDir?: string, dryRun: boolean = false ): Promise<WorkflowResult> { try { const git = getGit(workingDir); if (dryRun) { return { success: true, message: "Dry run: Would commit and push changes", details: { files, commitMessage, branch, workingDir } }; } // Check if we're in a git repository const isRepo = await git.checkIsRepo(); if (!isRepo) { return { success: false, message: "Failed to commit and push", error: "Not a Git repository" }; } // Get current status const status = await git.status(); // Create branch if specified and doesn't exist if (branch) { try { await git.checkoutBranch(branch, 'HEAD'); } catch (error) { // Branch might already exist, try to switch to it try { await git.checkout(branch); } catch (switchError) { return { success: false, message: "Failed to commit and push", error: `Failed to create or switch to branch ${branch}: ${switchError}` }; } } } // Add specified files if (files.length > 0) { await git.add(files); } else { // Add all modified files if no specific files provided await git.add('.'); } // Commit changes const commitResult = await git.commit(commitMessage); // Push changes const currentBranch = await git.revparse(['--abbrev-ref', 'HEAD']); await git.push('origin', currentBranch); return { success: true, message: "Successfully committed and pushed changes", details: { commit: commitResult.commit, branch: currentBranch, files: commitResult.summary.changes, insertions: commitResult.summary.insertions, deletions: commitResult.summary.deletions } }; } catch (error: any) { return { success: false, message: "Failed to commit and push", error: error.message }; } }
- src/index.ts:422-449 (schema)Input schema defining parameters for the git_commit_and_push tool.inputSchema: { type: "object", properties: { files: { type: "array", items: { type: "string" }, description: "Array of file paths to commit (empty for all changes)" }, commitMessage: { type: "string", description: "Commit message (use conventional commit format)" }, branch: { type: "string", description: "Branch name to create/switch to (optional)" }, workingDir: { type: "string", description: "Working directory path (defaults to current directory)" }, dryRun: { type: "boolean", description: "Preview changes without executing", default: false } }, required: ["commitMessage"] }
- src/index.ts:419-450 (registration)Tool registration in the ListToolsRequestHandler, including name, description, and input schema.{ name: "git_commit_and_push", description: "Commit staged changes and push to remote repository", inputSchema: { type: "object", properties: { files: { type: "array", items: { type: "string" }, description: "Array of file paths to commit (empty for all changes)" }, commitMessage: { type: "string", description: "Commit message (use conventional commit format)" }, branch: { type: "string", description: "Branch name to create/switch to (optional)" }, workingDir: { type: "string", description: "Working directory path (defaults to current directory)" }, dryRun: { type: "boolean", description: "Preview changes without executing", default: false } }, required: ["commitMessage"] } },
- src/index.ts:585-593 (registration)Dispatch to handler in the CallToolRequestHandler switch statement.case "git_commit_and_push": result = await commitAndPush( (args?.files as string[]) || [], args?.commitMessage as string, args?.branch as string, args?.workingDir as string, (args?.dryRun as boolean) || false ); break;
- src/index.ts:137-139 (helper)Helper function to get SimpleGit instance for the working directory, used by commitAndPush.function getGit(workingDir?: string): SimpleGit { return simpleGit(workingDir || process.cwd()); }