Commit messages in Git are often neglected. Many see them merely as a necessary evil to save changes. Meaningful commit messages are a decisive factor for the success of a project: they ensure good traceability of changes, efficient team collaboration and faster troubleshooting. No reason to have the commit messages created automatically with the help of AI.
With the help of so-called. Hooks it is possible to execute scripts for specific events during the Git workflow. The prepare-commit-msg hook is executed immediately. before The following script only needs to be executed in the folder ~/git-template/hooks/prepare-commit-msg
be filed:
0d311d3e3743f65bd1f7fa370c3b3848
Finally, you add your OpenAI API key in line 4 and, if desired, modify the model used in line 5 (in our example gpt-4o). We then make the script executable with chmod +x ~/git-template/hooks/prepare-commit-msg
and set with git config --global core.hooksPath ~/git-template/hooks
the default directory for Git hooks in the global Git configuration.
The script automatically generates a suitable template for the commit message for the diff of all tagged changes: git add -A . && git commit
. Alternatively, you can also send an empty message ("."), which is then overwritten: git add -A . && git commit -m "." && git push
.
The script also works in Git UI tools such as Tower and SmartGit. If you want to deactivate the behavior again, simply press git config --global --unset core.hooksPath
.