---
title: "git-gpt"
date: 2023-12-10T12:04:27.000Z
author: Z.SHINCHVEN
tags: [git, GPT, prompts]
canonical: https://atlassc.net/2023/12/11/git-gpt
---
<div style="display: flex; justify-content: center;">
  <img src="https://github.com/ShinChven/git-gpt/raw/main/assets/generate-commit-message.webp" alt="git-gpt">
</div>

## Introduction

By studying GPT, I wrote a GPT-based cli tool called [git-gpt](https://github.com/ShinChven/git-gpt) to help me manage my git repositories, specifically to help me write better commit messages.

The basic idea is to use GPT to generate commit messages based on the changes in the working directory.

## Prompts

```markdown
Analyze staged diffs:
[insert_diff]

Craft a conventional commit message in [insert_language] with a tagged title under 50 characters and a list of details about changes under 70 characters. Use appropriate tag (e.g., 'feat:', 'fix:', 'docs:', 'style:', 'refactor:', 'test:', 'chore:', etc.). Only provide the commit message and details.
```


## Python Snippet

```py
# Define the language of the commit message
lang = 'English'

repo = git.Repo(os.getcwd())
diffs = repo.git.diff('--staged')  # Get textual representation of staged diffs

prompt = f"Analyze staged diffs:\n```diff\n[insert_diff]\n```\nCraft a conventional commit message in [insert_language] with a tagged title under 50 characters and a list of details about changes under 70 characters. Use appropriate tag (e.g., 'feat:', 'fix:', 'docs:', 'style:', 'refactor:', 'test:', 'chore:', etc.). Only provide the commit message and details."

# replace [insert_diff] with the actual diffs
prompt = prompt.replace('[insert_diff]', diffs)
# replace [insert_language] with the target language
prompt = prompt.replace('[insert_language]', lang)
```

## Installation

Install `git-gpt` via pip:

```bash
pip install git+https://github.com/ShinChven/git-gpt.git#egg=git-gpt
```

Upgrade:

```bash
pip install --upgrade git+https://github.com/ShinChven/git-gpt.git#egg=git-gpt
```

## Configuration

Before using `git-gpt`, you'll need to configure it with your OpenAI API key and other optional settings. Run the following command and follow the prompts:

```bash
git-gpt config --api-key <API_KEY>
```

### Options:
- `--api-key`: The API key to use with OpenAI.
- `--base`: The alternative OpenAI host.
- `--model`: The model to use for generating messages.
- `--lang`: Target language for the generated message (default is 'en').
- `--issue-max-tokens`: The maximum number of tokens to use for the issue prompt.

## Usage

### Generating Commit Messages

Stage all changes and generate a commit message using GPT model:

```bash
git-gpt commit [--lang <LANGUAGE>] [--model <GPT-MODEL>] [--run-dry]
```

Options:

- `--lang`: Target language for the generated message (default is 'en').
- `--model`: The model to use for generating messages (default is 'gpt-3.5-turbo').
- `--run-dry`: Print the generated message without committing.
