Automating Git Commit Messages with GPT-3 for Faster Software Development Workflows

My Python script can write better commit messages than you ;) Just kidding, but they are pretty good.

We can run the script when the current working directory contains a git repository, and there are staged commits. The script will generate a commit message based on the staged commits and commit them.

Here is the script:

import subprocess
import openai

def run_command(command):
    process =, shell=True, capture_output=True, text=True)
    if process.returncode != 0:
        raise Exception(f'Command {command} failed with exit code {process.returncode}')
    return process.stdout

def check_if_commits_are_staged():
        result = run_command('git diff --staged')
        if result == '':
            return False
    except Exception:
        return False
    return True

def generate_commit_message_from_diff(diff):
    prompt = f"""Given the following git patch file:

    Generate a one-sentence long git commit message.
    Return only the commit message without comments or other text.

    response = openai.Completion.create(
        prompt=prompt, temperature=0,
    message = response['choices'][0]['text']
    return message.strip().replace('"', '').replace("\n", '')

if __name__ == '__main__':
    if not check_if_commits_are_staged():
        print('No staged commits')
    diff = run_command('git diff --staged')
    commit_message = generate_commit_message_from_diff(diff)
    run_command(f'git commit -m "{commit_message}"')
    print(f'Committed with message: {commit_message}')


Before we run the script, we need to install the OpenAI Python library and set the API key environment variable.

When we finish the setup, we can modify files in the project directory, stage them for commit and run the script.


$ python
Committed with message: Update to return the text of the first choice in the response.

In the git log, we see the commit message generated by the script:

commit f1f7da697aa3e7de634d598a4cc43695ad4d7fca (HEAD -> main)
Author: Bartosz Mikulski <xxx>
Date:   Tue Feb 14 19:25:35 2023 +0100

        Update to return the text of the first choice in the response.

How does it work?

The script uses the OpenAI API to generate a commit message based on the staged commits. We use the git diff --staged command to get the staged commits and pass them to the OpenAI API.

If the command returns an empty response, we know there are no staged commits, and we can exit the script. It will also fail if the current working directory is not a git repository.

In the next step, we get the git patch file from the git diff --staged command and pass it to the OpenAI API.

In the prompt, I instructed it to generate a one-sentence long commit without adding any comments. It’s crucial to add the “Return only the commit message without comments or other text.” instruction to the prompt. Otherwise, it tends to start the response with “Here is the commit message,” “For the given changes we can generate the following commit message,” or “The commit message is.” We don’t want it in our commit messages.

We don’t want double quotes or new lines in the message, either. However, removing them in the Python script is easier than asking GPT-3 not to generate them.

After getting the commit message, we pass it as the message argument to the git commit command.

Older post

Connect GPT-3 to the Internet: Create a Slack Bot and Perform Web Search, Calculations, and More

Unleash the potential of GPT-3 and make it access the Internet. Learn how to use Langchain and build a Slack bot that can do a web search, extract text from websites, and perform calculations.

Newer post

Detection of Text Duplicates and Text Search with Word Embeddings and Vector Databases

Discover how word embeddings and vector databases can revolutionize text search and duplicate detection. Learn how to implement it with OpenAI GPT-3 and Milvus vector database.