git_commit_and_push
Commit staged changes to Git with a message and push to remote repository. Supports file selection, branch management, and preview mode.
Instructions
Commit staged changes and push to remote repository
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| files | No | Array of file paths to commit (empty for all changes) | |
| commitMessage | Yes | Commit message (use conventional commit format) | |
| branch | No | Branch name to create/switch to (optional) | |
| workingDir | No | Working directory path (defaults to current directory) | |
| dryRun | No | Preview changes without executing |
Implementation Reference
- src/index.ts:144-231 (handler)The main handler function that executes the git commit and push logic. Uses simple-git to check repo, handle branches, add files, commit with message, and push to origin.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 definition for the git_commit_and_push tool, specifying parameters like files, commitMessage (required), branch, workingDir, and dryRun.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)Registration in the tool dispatcher switch statement, mapping the tool name to the commitAndPush handler function call.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:419-450 (registration)Tool registration in the ListTools response, 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:137-139 (helper)Helper function to get a SimpleGit instance for the specified working directory, used in the handler.function getGit(workingDir?: string): SimpleGit { return simpleGit(workingDir || process.cwd()); }