Skip to content

Instantly share code, notes, and snippets.

@fpytloun
Last active October 13, 2025 15:52
Show Gist options
  • Save fpytloun/458304b2a6bf59b9e534f295ccfebe4b to your computer and use it in GitHub Desktop.
Save fpytloun/458304b2a6bf59b9e534f295ccfebe4b to your computer and use it in GitHub Desktop.
gpt-commit function
## Custom functions
function gpt-commit() {
local amend=0
local push=0
for arg in "$@"; do
case "$arg" in
--help|-h)
echo "Usage: gpt-commit [--amend] [--push] [--help|-h]"
echo " --amend Amend the last commit (optionally reword)"
echo " --push Push to tracked remote/branch after commit"
echo " --help|-h Show this help message"
return 0
;;
--amend)
amend=1
;;
--push)
push=1
;;
-*)
echo "gpt-commit: unknown option '$arg'" >&2
echo "Run 'gpt-commit --help' for usage." >&2
return 2
;;
# all other args are ignored
esac
done
local commit_status=128
local staged
local msg
local remote branch
staged=$(git diff --cached)
if [[ -z "$staged" && $amend -eq 0 ]]; then
echo "No staged changes to commit!" >&2
return 1
fi
if [[ $amend -eq 1 ]]; then
if [[ -z "$staged" ]]; then
# No staged changes, just reword last commit
msg=$(git show --pretty=format:%B HEAD | gptsh -c ~/.config/gptsh/config.yml --agent committer "Generate single commit message based on following diff:")
git commit --amend --edit -m "$msg"
commit_status=$?
else
# Staged changes and committed changes
local last_commit_diff
last_commit_diff=$(git show --pretty=format:'' HEAD)
msg=$( (echo "$last_commit_diff"; echo "$staged") | gptsh -c ~/.config/gptsh/config.yml --agent committer "Generate single commit message based on following diff:")
git commit --amend --edit -m "$msg"
commit_status=$?
fi
else
msg=$(echo "$staged" | gptsh -c ~/.config/gptsh/config.yml --agent committer "Generate single commit message based on following diff:")
git commit --edit -m "$msg"
commit_status=$?
fi
if [[ $push -eq 1 && $commit_status -eq 0 ]]; then
upstream=$(git rev-parse --abbrev-ref --symbolic-full-name @{u})
remote=${upstream%%/*}
branch=${upstream#*/}
git push "$remote" HEAD:"$branch"
commit_status=$?
fi
return $commit_status
}
_gpt_commit_completion() {
local -a opts
opts=(
'--amend:Amend the last commit'
'--push:Push after commit'
'--help:Show help'
'-h:Show help'
)
_describe 'option' opts
}
compdef _gpt_commit_completion gpt-commit
@fpytloun
Copy link
Author

  1. Install gptsh, make sure ~/.local/bin is in your $PATH
uv tool install gptsh
  1. Configure gptsh including example committer agent. Take examples directory content and place into ~/.config/gptsh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment