Automate your Git Workflow using AI


Tired of thinking what comments to add for your git commit. This flow will help you.
Prerequisites
Git Bash: Installed on your Windows System.
Ollama CLI: Installed and configured with
phi3.5
model.Basic Knowledge of Git Commands and Terminal Usage.
Step 1: Create the Script File
Open your favorite Text editor and create a new file.
Copy and paste the following Bash Script:
#!/bin/bash # git_auto_commit.sh if ! git rev-parse --git-dir > /dev/null 2>&1; then echo "Error: This directory is not a Git repository." exit 1 fi if git diff-index --quiet HEAD --; then echo "No changes detected. Exiting." exit 0 fi echo "Staging changes..." git add . summary=$(git diff --cached --name-only) echo "Generating commit message suggestion from Ollama phi3.5..." suggested_commit_msg=$(ollama run phi3.5 ""Generate a concise yet informative commit message that accurately summarizes the following code changes. The message should be clear, professional, and follow best practices for commit messages. Aim to describe the purpose and impact of the changes in a way that is easy to understand. Avoid unnecessary details but ensure the key modifications are covered. Format the message in an imperative style (e.g., 'Fix bug in authentication flow' instead of 'Fixed a bug...'). Here are the changes to summarize: $summary") echo "Suggested commit message:" echo "$suggested_commit_msg" read -p "Edit commit message? (y/n): " choice if [[ "$choice" =~ ^[Yy]$ ]]; then echo "Enter your commit message below. Press Ctrl+D when done." commit_msg=$(cat) # Reads multiline input until Ctrl+D else commit_msg="$suggested_commit_msg" fi git commit -m "$commit_msg" git push origin master echo "Done!"
Save the file as
git_auto_commit.sh
.
Step 2: Add the Script to Your Global Bin Directory
Create a
bin
directory if it doesn’t exist:mkdir -p ~/bin
Move the script to this directory:
mv /path/to/git_auto_commit.sh ~/bin/
Step 3: Make the Script Executable inside a bash terminal.
You can use Git Bash as well to execute it here
chmod +x ~/bin/git_auto_commit.sh
Step 4: Add the Script Directory to Your PATH
Open
~/.bashrc
or~/.bash_profile
:nano ~/.bashrc
Add this line at the end:
export PATH="$HOME/bin:$PATH"
Save and apply changes:
source ~/.bashrc
Step 5: Testing the Script
Navigate to a Git repository.
Run:
git_auto_commit.sh
Follow the prompts.
Additional Notes
Ensure
Ollama
is installed and running.Windows NTFS permissions may not reflex Unix-style
chmod
changes, but the script will execute.
This script is now available at my GitHub Repository. Go ahead and automate your Git Workflow.
Subscribe to my newsletter
Read articles from Aditya Trivedi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
