Skip Navigation
ShareGPT @reddthat.com

How do you prompt an LLM to write high-quality Git commit messages?

Writing a good Git commit message is a skill, and prompting a large language model (LLM) to do it effectively is about giving it clear context, structure, and intent. Here's a detailed guide on how to craft prompts for this purpose.


1. Give the LLM context about the changes

LLMs need to know what changed and why. You can provide:

  • A diff or a summary of changes
  • The file names and their purpose
  • Any bug IDs, tickets, or issue numbers

Example:

 text
    
I modified `app.py` to fix a bug where the login fails if the username has special characters. Also updated `requirements.txt` to include `validators` library.

  

2. Specify the style of commit message

Good commit messages often follow the Conventional Commits style:

 
    
<type>(<scope>): <short summary>
<BLANK LINE>
<longer description>

  

Where type can be:

  • feat: new feature
  • fix: bug fix
  • chore: maintenance
  • docs: documentation

You can prompt the LLM to adhere to this style:

 text
    
Write a Git commit message following the Conventional Commits format. Keep the first line under 50 characters, and include a detailed description if necessary.

  

3. Include an example prompt

Here’s a full example prompt you could use:

 text
    
I made the following changes in this commit:
- Fixed login bug when the username contains special characters.
- Updated `requirements.txt` to include the `validators` library.

Write a clear, concise, and professional Git commit message in the Conventional Commits style, including:
- A short summary under 50 characters
- A longer description if necessary explaining the reason for the change
- Reference the issue #123 if relevant

  

Expected output from the LLM:

 
    
fix(auth): handle special characters in username

Login was failing when usernames contained special characters.
Added validation using the `validators` library to prevent this issue.

Closes #123

  

4. Tips for better prompts

  1. Include diff snippets if you want extremely precise commit messages.
  2. Ask for multiple options:
     text
        
    Provide 3 alternative Git commit messages for these changes.
    
      
  3. Ask for formal or casual tone, depending on your team style.
  4. Include issue references for linking commits to tickets.

If you want, I can write a reusable LLM prompt template that you can plug into any repo to automatically generate high-quality commit messages from diffs. It would save a ton of time.

Do you want me to do that?

1 comments