Automated Git Commit Messages
In the world of software development, making consistent and informative commit messages is crucial but often overlooked. This task can become tedious, especially when you are in the flow of coding. To help with this, we’ve crafted a script tailored to automate generating Git commit messages, ensuring they are meaningful and save you time.
The script acts as a regular node.js automation script and uses runPrompt to issue calls to the LLM and ask the user to confirm the generated text.
Explaining the Script
First, we check if there are any staged changes in the Git repository:
If no changes are staged, we ask the user if they want to stage all changes. If the user confirms, we stage all changes. Otherwise, we bail out.
We generate an initial commit message using the staged changes:
The prompt configuration above indicates that the message should be concise, related to the “git diff —cached” output, and should not include quotes.
User chooses how to proceed with the generated message:
Options are given to edit or regenerate the message. If the user chooses to edit the message, we ask them to input a new message:
If the user chooses to commit the message, we commit the changes:
Running the Script
You can run this script using the CLI.
You can wrap this command in a gcm.sh
file or in your package script
section in package.json
:
Then you can run the script using:
Using git hooks
You can also attach to the commit-msg git hook to run the message generation on demand.
Using the huksy framework, we can register the execution
of genaiscript in the .husky/commit-msg
file.
The commit-msg
hook receives a file location where the message is stored. We pass this parameter to the script
so that it gets populated in the env.files
variable.
In the script, we check if the content of the file already has a user message, otherwise generate a new message.
Acknowledgements
This script was inspired from Karpathy’s commit message generator.