Created
October 20, 2025 04:31
-
-
Save MiguelMachado-dev/14f7e95f996f554c45403a27ad424491 to your computer and use it in GitHub Desktop.
Git commit using AI Gemini CLI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # First, you need to open your profile file. Run this command in PowerShell to open it in vscode(it will be created if it doesn't exist): | |
| if (!(Test-Path $PROFILE)) { New-Item -Path $PROFILE -Type File -Force }; code $PROFILE ## YOU SHOULD NOT KEEP THIS LINE IN THE PROFILE FILE LOL | |
| # Add the code below and then refresh your profile $ . $PROFILE | |
| function Git-CommitAI { | |
| $prompt = @' | |
| Write a commit message in the Conventional Commits format. Use the structure: <type>(<optional scope>): <short description> <optional body> <optional footer> Example types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert Optionally, include a body for more details in bullet points. Optionally, in the footer, use BREAKING CHANGE: followed by a detailed explanation of the breaking change. Just return the commit message, do not include any other text. | |
| '@ | |
| # Get the full multi-line message from Gemini | |
| $fullMessage = (git diff --cached | gemini -p $prompt).Trim() | |
| # Check if a message was actually generated | |
| if ([string]::IsNullOrWhiteSpace($fullMessage)) { | |
| Write-Host "⚠️ No commit message generated. Aborting commit." -ForegroundColor Yellow | |
| return # Exit the function | |
| } | |
| $tempFile = [System.IO.Path]::GetTempFileName() | |
| try { | |
| Set-Content -Path $tempFile -Value $fullMessage -Encoding UTF8 | |
| git commit -F $tempFile | |
| } finally { | |
| # Clean up and delete the temporary file afterwards | |
| if (Test-Path $tempFile) { | |
| Remove-Item $tempFile | |
| } | |
| } | |
| if ($LASTEXITCODE -eq 0) { | |
| git --no-pager log --stat -1 | |
| } | |
| } | |
| # Create a short alias 'ca' for the function | |
| Set-Alias -Name ca -Value Git-CommitAI |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment