Git commit messages with ChatGPT

Commit messages in Git are often neglected. Many see them merely as a necessary evil to save changes. Yet 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 prepared automatically with the help of AI.


With the help of so-called. Hooks it is possible to execute scripts, e.g. for automated testing or code checking for certain events during the Git workflow. The client-side prepare-commit-msg-Hook is immediately before The following bash script, for example, must 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, modifies the ChatGPT model used in line 5 (in our example gpt-4oWe 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 generates for the diff of all tagged files automatically creates a suitable template for the commit message: git add -A . && git commit. Alternatively, you can also send an empty message ("."), which is then automatically overwritten: git add -A . && git commit -m "." && git push – but be careful: validation of the message is no longer possible here.

The AI models are now so advanced that very reasonable automatic commit messages are created based on the changes. By adjusting the prompt (from line 13) you can further optimize the return. By the way, the script also works in Git UI tools like Tower and SmartGit. If you want to deactivate the behavior again, simply press git config --global --unset core.hooksPath.

Back