- Posted on
- • Artificial Intelligence
Artificial Intelligence Git Workflows
- Author
-
-
- User
- linuxbash
- Posts by this author
- Posts by this author
-
Artificial Intelligence Git Workflows: A Bash-First Playbook
Your Git history is your team’s memory. But between rushed commits, vague messages, and noisy reviews, that memory gets fuzzy fast. AI can help you keep it sharp—right from your terminal—without bloating your stack or breaking your Bash flow.
This guide shows how to weave AI into your Git workflow to write better commits, review diffs quickly, enforce standards, and leave useful breadcrumbs for your future self. You’ll get practical Bash snippets, real-world hooks, and distro-agnostic install steps.
Why AI in Git is worth it
Precision and speed: AI can produce clear, policy-compliant commit messages and PR summaries in seconds.
Consistency at scale: Enforce Conventional Commits and team voice without endless nitpicks.
Better on-call context: Summaries and notes reduce paging-to-context time when you’re firefighting.
Works with your CLI: Keep your terminal workflow; add AI where it removes friction, not adds it.
1) One-command AI commit messages (using aicommits)
aicommits reads your staged changes and drafts a solid commit message (supports Conventional Commits, emojis optional).
Installation (OS packages + Node.js + aicommits):
Debian/Ubuntu (apt):
sudo apt update sudo apt install -y git nodejs npm sudo npm install -g aicommitsFedora/RHEL/CentOS (dnf):
sudo dnf install -y git nodejs npm sudo npm install -g aicommitsopenSUSE (zypper):
sudo zypper refresh sudo zypper install -y git nodejs npm sudo npm install -g aicommits
Environment (set once; choose your provider):
export OPENAI_API_KEY="sk-...your-key..."
# or: export ANTHROPIC_API_KEY="..."
Usage:
git add -A
aicommits -m
Tip: Create a tiny wrapper so teammates get the same behavior:
mkdir -p ~/.local/bin
cat > ~/.local/bin/ai-commit <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
git add -A
# Add flags like --type=conventional if your team uses Conventional Commits
aicommits -m "$@"
EOF
chmod +x ~/.local/bin/ai-commit
Then:
ai-commit
Value: You get consistent, descriptive messages that make git log and PR history actually useful.
2) A Bash-friendly AI helper for diffs, reviews, and PR text
Skip heavyweight tooling and call an OpenAI-compatible API with curl. We’ll write a small ai function that:
Works with stdin or a prompt.
Respects OPENAI_API_KEY.
Is easy to vendor into any repo.
Install jq (for clean JSON handling):
Debian/Ubuntu (apt):
sudo apt update sudo apt install -y jqFedora/RHEL/CentOS (dnf):
sudo dnf install -y jqopenSUSE (zypper):
sudo zypper refresh sudo zypper install -y jq
Add this to your shell profile (e.g., ~/.bashrc):
ai() {
set -euo pipefail
: "${OPENAI_API_KEY:?Set OPENAI_API_KEY first}"
local model="${AI_MODEL:-gpt-4o-mini}"
local base="${OPENAI_BASE_URL:-https://api.openai.com}"
local prompt="${1:-}"
local stdin=""
if [ ! -t 0 ]; then stdin="$(cat)"; fi
local content="$prompt"
if [ -n "$stdin" ]; then
content="$prompt
----
$stdin"
fi
curl -s "$base/v1/chat/completions" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d "$(jq -nc --arg m "$model" --arg c "$content" \
'{model:$m,temperature:0.2,messages:[{role:"user",content:$c}]}')" \
| jq -r '.choices[0].message.content'
}
Examples:
Summarize staged changes before you commit:
git diff --staged | ai "Summarize the intent, risks, and testing steps for this diff in 5 concise bullets."Draft a PR description:
git log --pretty=format:'- %s' origin/main..HEAD | ai "Turn these commit titles into a crisp PR description with a short overview, changes list, and testing notes."Ask for a quick review on a file you touched:
ai "Spot potential edge cases or shell pitfalls in this script:" < path/to/script.sh
Value: Instant, context-aware help—no browser context-switching.
3) Enforce and improve Conventional Commits with AI assist
Pair a standard (Conventional Commits) with AI that auto-fixes messages when they miss the mark.
Install Commitizen (optional but helpful):
Debian/Ubuntu (apt) + Node.js from earlier:
sudo npm install -g commitizenFedora/RHEL/CentOS (dnf):
sudo npm install -g commitizenopenSUSE (zypper):
sudo npm install -g commitizen
Initialize (one-time, in your repo):
npm pkg set config.commitizen.path=cz-conventional-changelog
npm install --save-dev cz-conventional-changelog
echo '{ "path": "cz-conventional-changelog" }' > .czrc
Use it:
cz commit
Optional: Auto-rewrite non-conforming messages with a commit-msg hook:
cat > .git/hooks/commit-msg <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
file="$1"
regex='^(feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert)(\(.+\))?: .+'
if ! grep -Eq "$regex" "$file"; then
if [ -n "${OPENAI_API_KEY:-}" ]; then
new="$(ai "Rewrite to a single-line Conventional Commit (no emojis, present tense). Only output the message line." < "$file" || true)"
if [ -n "${new:-}" ]; then printf '%s\n' "$new" > "$file"; fi
fi
fi
EOF
chmod +x .git/hooks/commit-msg
Value: You keep a clean, machine-parseable history—crucial for changelogs, release notes, and analytics.
4) Quality gates with pre-commit to reduce AI churn
Static analyzers catch low-level issues so AI can focus on higher-level clarity. Wire them into pre-commit and stop bad changes before they start.
Install tools:
pre-commit
- Debian/Ubuntu (apt):
sudo apt update && sudo apt install -y pre-commit - Fedora/RHEL/CentOS (dnf):
sudo dnf install -y pre-commit - openSUSE (zypper):
sudo zypper refresh && sudo zypper install -y pre-commit
- Debian/Ubuntu (apt):
shellcheck
- Debian/Ubuntu (apt):
sudo apt install -y shellcheck - Fedora/RHEL/CentOS (dnf):
sudo dnf install -y ShellCheck - openSUSE (zypper):
sudo zypper install -y ShellCheck
- Debian/Ubuntu (apt):
shfmt
- Debian/Ubuntu (apt):
sudo apt install -y shfmt - Fedora/RHEL/CentOS (dnf):
sudo dnf install -y shfmt - openSUSE (zypper):
sudo zypper install -y shfmt
- Debian/Ubuntu (apt):
codespell (optional)
- Debian/Ubuntu (apt):
sudo apt install -y codespell - Fedora/RHEL/CentOS (dnf):
sudo dnf install -y codespell - openSUSE (zypper):
sudo zypper install -y codespell
- Debian/Ubuntu (apt):
Add config (.pre-commit-config.yaml):
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-added-large-files
- repo: https://github.com/koalaman/shellcheck-precommit
rev: v0.10.0
hooks:
- id: shellcheck
args: ["-S", "style"]
- repo: https://github.com/mvdan/sh
rev: v3.10.0
hooks:
- id: shfmt
args: ["-i", "2", "-ci"]
- repo: https://github.com/codespell-project/codespell
rev: v2.3.0
hooks:
- id: codespell
args: ["-L", "crate,ser,ba"] # customize ignore list
Enable:
pre-commit install
pre-commit run -a
Value: You spend less time on lint nits and more time on meaningful reviews and commit clarity.
5) Capture AI rationale in Git notes (not your commit body)
Keep commit messages clean and still store rich reasoning using Git notes. They don’t pollute diffs and can be pushed/pulled like refs.
Example:
git add -A
git commit -m "feat(api): add pagination to list endpoint"
git notes add -m "$(git diff --staged | ai 'In 3–5 bullets, explain intent, trade-offs, and test coverage for these changes.')"
# Share notes with your remote:
git push origin refs/notes/commits:refs/notes/commits
View later:
git log --show-notes=commits -n 1
Value: You preserve decision context without turning commit messages into essays.
Practical safeguards
Don’t leak secrets: Never feed keys, tokens, or customer data to any external AI. Add
.env,.secrets, and artifacts to.gitignore.Local or private models: If policy forbids cloud, point
OPENAI_BASE_URLat your internal, OpenAI-compatible gateway or a self-hosted API. Theaifunction will Just Work if the API is compatible.Make it optional: Let teammates opt in by checking for
OPENAI_API_KEYin hooks and scripts.
Conclusion and next steps
Better Git hygiene compounds over time. Start small:
Today: Add the
aifunction and summarize your next diff.This week: Adopt
aicommitsfor consistent messages.This month: Wire
pre-commitgates and acommit-msgfixer to enforce Conventional Commits.When ready: Store AI rationale in Git notes for high-signal history.
Your future self—and your team—will thank you.
If you found this useful, try rolling these snippets into a team-ready dotfiles repo or a template project. Have a favorite AI + Git trick? Share it and we’ll feature the best Bash workflows in a follow-up.