automating git commit messages with gpt
have you ever wanted to commit your changes on git, but found yourself to be too tired or a bit too much in a hurry to think of a good commit message? too much in the zone and don’t want to interrupt your flow?
to scratch that itch, i’ve made a script called gpt_commit that automates the generation of commit messages using openai.
how it works
the script retrieves the diff of your staged changes and sends it to the openai with a prompt to generate a commit message. it then provides you with options to accept the generated message, edit it, regenerate a new one, or abort the commit.
here’s a simplified overview of the process:
- stage your changes:
git add . - run the script:
gpt_commit - interact with the generated message:
Generated commit message: "Fix authentication bug and improve error handling" Options: (y) Accept and commit (e) Edit message (r) Regenerate message (q) Abort Choose an option [y/e/r/q]:
prerequisites
- openai api key: obtain one from openai.
- jq: for json processing.
- install with homebrew:
brew install jq - or with apt:
sudo apt-get install jq - or get it at github.com/jqlang/jq
- install with homebrew:
installation
one way to install gpt_commit is as follows:
-
clone the repository:
git clone https://github.com/strathausen/gpt_commit.git -
navigate to the directory:
cd gpt_commit -
make the script executable:
chmod +x gpt_commit -
add the script to your path:
export PATH="$PATH:$(pwd)"you might want to add this line to your
~/.bashrcor~/.zshrcfile.
or just copy the script from github and put it whereever you like.
configuration
set your openai api key as an environment variable:
export OPENAI_API_KEY="your-openai-api-key"
what’s next
options to change models or add a custom prompt would be nice. especially on retry it could ask for another prompt on how to improve the message. providing more context to the llm is another idea: what is this codebase about? which commits preceded this one?
initially i had mixed feelings about the whole idea: will this script just contribute to the amount of generated sludge coming from ai lately? then i looked at the commit messages of me and my team and thought, whatever generated sludge the ai will come up with, it’s almost certainly more helpful than that.
check out the repository on github: strathausen/gpt_commit
feel free to contribute or open issues!