How to generate git commit messages using GPT-4

In this tutorial, you'll be given a bash script to help you generate git commit messages using ChatGPT / GPT-4 API based on your git diff message

Share:

Apr 1, 2023 286 Words

Read Time: 2 Minutes

A mockup of the blog post named "How to generate git commit messages using GPT-4" by TnvMadhav.

Setup & Requirments

🧠 I’ve built this on the macOS and I’ve written this guide for linux based operating system

Install jq using Home Brew

brew install jq

Generate OpenAPI Key

  • Go to the OpenAI website and signup if you don’t already have an account
  • From the OpenAI dashboard, click the “API keys” link in the top menu
  • Click the “Create” button to generate your API key and copy it
  • Store key in an environment variable called OPENAI_API_KEY for use while running bash script later

Get Bash Script

#!/bin/bash

# Replace double quotes with Unicode character
function replace_double_quotes {
  echo "${1//\"/\u201C}"
}

# Replace single quotes with Unicode character
function replace_single_quotes {
  echo "${1//\'/\u2018}"
}

# Get the Git diff and save it to a file
STRING_WITH_BREAKS=$(git diff)

# Read the API key from an environment variable
API_KEY=$OPENAI_API_KEY

echo "With breaks $STRING_WITH_BREAKS \n"

STRING_NO_BREAKS=$( echo "Generate a commit message based on this Git diff: $STRING_WITH_BREAKS" | tr -d '\n')

# Solves convoluted escape issues:  spent 2 hours on this problem :(
STRING_NO_BREAKS=$(replace_double_quotes "$(replace_single_quotes "$STRING_NO_BREAKS")")

echo "No breaks $STRING_NO_BREAKS \n"

# URL Encode the string
STRING_URL_ENCODE=$(echo "$STRING_NO_BREAKS" | jq -sRr @uri)

echo "Url encode $STRING_URL_ENCODE \n"

if [ -z "$API_KEY" ]; then
  echo "ERROR: OPENAI_API_KEY environment variable is not set"
  exit 1
fi

# Call the ChatGPT API to generate a commit message based on the diff
CURL_COMMAND="curl --location 'https://api.openai.com/v1/chat/completions' \
 --header 'Content-Type: application/json' \
 --header 'Authorization: Bearer $API_KEY' \
 --data-raw '{\"model\": \"gpt-4\", \"messages\": [{\"role\": \"user\", \"content\": \"$STRING_URL_ENCODE\" }]}'"

echo "Curl command is $CURL_COMMAND"

COMMIT_MSG=$(eval "$CURL_COMMAND" | jq -r ".choices[].message.content")

# Add and commit the changes with the generated commit message
git add .
git commit -m "$COMMIT_MSG"

echo "Changes committed with message: $COMMIT_MSG"

👋 – @TnvMadhav

Find more posts from following topics

accurate-requests
api-development
api-testing
api-testing-tools
array
automated-testing
bad-habits
base64-decoder
base64-encoder
binding
blog
blogging
bulma-css
bulma.io
button-swiftui
chatgpt
clipboard
code
code-block
code-snippet
comparison
compile
configuring-debugger-for-django-in-vs-code
configuring-launch.json-for-python-debugger
copy
copy-to-clipboard
copy-to-clipboard-neovim
css
current-date
current-time
current-timestamp
debugger-setup-in-visual-studio-code
debugging-django-app-in-visual-studio-code
debugging-python-code-in-visual-studio-code
debugging-python-programs-with-visual-studio-code
debugging-python-with-virtual-environment-in-vs-code
developer-productivity
developers
development-workflow
dom
dynamic-sitemap-in-nextjs
engineering-dashboard
flowcharts
git
git-diff
github
global-keyboard-shorcut
global-shortcut
go
go-hugo
go-programming
go-to-line
golang
golang-development
good-habits
gorilla-websocket
gpt
gpt-3.5
gpt-4
gpt-4-api
guide
gumroad
habits
habits-tracker-notion-template
hamburger-menu
hotkeys
html
hugo
ide
image
image-sharing
image-tool-for-ios
imagerenderer
include-timestamp
integrated-development-environment
ios
ios-16
ios16
javascript
keyboard-shortcut
linux
macos
map
markdown
markdown-code
mental-programming
menu
menubarextra
mermaid-syntax
mistake-tracker-notion
mobile-view
modifier
navbar
navigationlink
navigationstack
neovim
next.js
nextjs
nextjs-markdown
nextjs-sitemap
nextjs-sitemaps
nice-shot
nice-shot-pro
notion
notion-api
notion-api-python
notion-budget
notion-budget-template
notion-budget-tracker
notion-bug-report-tracker
notion-dashboard
notion-expense-manager
notion-habits
notion-habits-dashboard
notion-habits-template
notion-habits-tracker
notion-habits-tracker-template
notion-issue-tracker
notion-mistake-tracker
notion-product
notion-product-dashboard
notion-product-roadmap
notion-product-roadmap-dashboard
notion-tasks
notion-tasks-dashboard
notion-tasks-template
notion-tasks-tracker
notion-template
notionworkspaces
openai
osx
personal-ifttt-framework
photospicker
photospickeritem
phpickerfilter
postman-capabilities
postman-request
pre-request-script
product-roadmap-notion-template
product-roadmap-template
productivity
programming
python
python-api
python-debugger-tutorial-for-vs-code
python-debugging-mode-in-vs-code
python-notion-api
real-time-communication
rehype
remark
request-data
running-debugger-in-visual-studio-code
running-django-app-in-debugging-mode
running-program-in-debugging-mode-in-vs-code
running-python-code-in-debugging-mode
screenshot-app-for-ios
screenshot-app-ios
screenshot-ios
screenshot-tool-for-ios
set-current-timestamp
setting-up-debugger-in-vs-code-for-python
share-extension
sharelink
sharepreview
sharesheet
simple-websocket-server
sitemap
slice
slider
step-by-step-guide
stocks-profits-tracker
stocks-profits-tracker-template
stocks-tracker
struct
sustained-vigilance
swift
swiftui
swiftui-button
swiftui-button-action
swiftui-button-style
table-of-contents
tasks-tracker-notion-template
textfield-swiftui
timeliness
timestamp-integration
transferable
triggers-and-actions
tutorial
us-stocks
usa-stocks
useful-ios-features
using-breakpoints-in-python-debugger
using-virtual-environment-with-python-debugger
vanilla-javascript
variable
vim
visual-mode
visual-studio-code
vs-code
vscode
vscode-go-to-line
web-sockets-in-go
websocket-client
websocket-programming
websocket-server
xcode