Skip to main content

Command Palette

Search for a command to run...

AI-Generated GIT Commit Messages: Locally-Hosted Options

Updated
6 min read
AI-Generated GIT Commit Messages: Locally-Hosted Options
C

Confidence is a technology enthusiast with an unyielding passion for knowledge acquisition and dissemination. When he's neither hacking tech stacks nor exploring the latest dev frameworks, he enjoys sharing educative pieces and playing the piano.

In a previous article, we introduced three AI tools you can use to generate Git commit messages with large language models such as OpenAI's GPT and Anthropic's Claude - each required server access and authentication.

For example, Microsoft Copilot needed you to authenticate sessions with your GitHub credentials. Alternative third-party packages required that you obtain an API key from supported LLM providers and were limited by API request and token usage restrictions.

Now you'll learn how to set up and use large language models locally for generating Git commit messages without restrictions.

We'll install Ollama and configure models to work with Git using a custom script. We'll also use helper packages with Ollama access in a typical development environment. Both methods can be used to generate commit messages in your project without an internet connection.

Important: It is crucial to note that containerized LLMs can be quite sizeable and resource-intensive. However, having them run locally instead of querying their cloud-hosted versions offers room for enhanced accessibility, flexibility, and control.

Setting up Ollama

Ollama is a popular open-source tool that lets you host and run large language models locally.

To get started with Ollama, you’ll need to install it on your device.

  • Visit the official Ollama download webpage and download the application for your operating system.

    Ollama
  • Run OllamaSetup.exe (Windows)

After installation, run Ollama by clicking the Ollama icon on the Windows Start menu.

Make sure Ollama is running in Windows background processes. You can confirm this by looking up the following address in your browser:

http://127.0.0.1:11434
ollama is running

or by using this command in the terminal.

$ curl http://127.0.0.1:11434

You can also use the Invoke-RestMethod on PowerShell (Windows) to confirm that Ollama is running.

$ Invoke-RestMethod http://127.0.0.1:11434

Installing Large Language Models

Now that you have Ollama downloaded and running on your machine, the next step is to install Ollama-compatible models.

You can install language models on Ollama using the Ollama CLI, which directly downloads models from the Ollama library.

Since our objective is primarily to summarize and generate texts based on code version differences, we'll use Mistral, a relatively lightweight language model popular for its text-generating capabilities.

Pull and Run LLMs with Ollama CLI (Mistral)

To install (pull) and run Mistral, use the following commands:

$ ollama pull mistral
$ ollama run mistral

The above works the same for all models listed in the Ollama model library. Replace mistral with the model name you wish to install and run.

powershell_SqEc8Meieu

Note: Downloading mistral might take some time so be patient while it pulls and writes the model's manifest to your pc.

Once this process is done, you'll see success returned in the terminal.

Enter /? in the command line to show all available Ollama commands.

Now you can begin interacting with Mistral. Try doing this with and without an internet connection.

powershell_2U3LWXReoK

Ollama is preconfigured to utilize CPUs for prompt processing and response by default. This means model response rate can vary. Responses can be painfully slow if your PC's CPU does not meet the minimum required threshold and vice versa. It is recommended that, if available, you leverage GPUs to enhance Ollama performance.

Ollama for Git Commit Message Generation with Bash script

Before configuring Ollama to work with Git, ensure you have either Git Bash or WSL terminal integrated into your IDE.

image

Proceed with the following steps:

  1. At the root of your project, create a new bash script mistral-ai-commit.sh.

    The code below generates concise commit messages using staged changes in your git repository. This is the content of your bash script.

    #!/bin/bash  
    
    # Gets the current branch name  
    branch_name=$(git symbolic-ref --short HEAD)  
    
    # Gets the current user name  
    user_name=$(git config user.name)  
    
    # Gets the status messages  
    git_diff=$(git diff --cached)  
    
    # Checks if there are any staged changes  
    if [ -z "$git_diff" ]; then  
        echo "No staged changes found. Please stage your changes before running this script."  
        exit 1  
    fi  
    
    # Creates a contextual prompt for the Ollama model  
    prompt="Generate a very concise git commit message based on the following changes. It should be a single sentence:\n$git_diff\n"  
    
    # Use Ollama mistral to generate commit message  
    commit_message=\((ollama run mistral "\)prompt")  
    
    # Checks if a commit message was generated  
    if [ -z "$commit_message" ]; then  
        echo "Failed to generate a commit message. Please check your Ollama setup."  
        exit 1  
    fi  
    
    # Display the generated commit message with spaces between responses  
    echo -e "\nSuggested commit message:\n\n$commit_message\n"  
    
    # Print optional information  
    echo "Branch: $branch_name"  
    echo "User: $user_name"
    
  2. Make the script executable with the following command.

    $ chmod +x mistral-ai-commit.sh
    
  3. Stage file changes in your project.

    $ git add <file1> <file2> ...
    

    or

    $ git add .
    
  4. Run the script to generate a commit message.

    $ ./mistral-ai-commit.sh
    
    Suggested commit message:
    
     Added script for generating concise git commit messages using Ollama (mistral-ai-commit.sh)
     
    Branch: local
    User: kingstondoesit
    
  5. Review the suggested commit message and commit changes.

In five simple steps, you've created and executed a reusable script that generates git commit messages locally with no authentication requirement. The great thing about this is that the prompt used in the script can be better fine-tuned to suit a specific style or annotation.

Ollama Git Commit with Helper packages

You do not always need to write specialized scripts to generate Git commit messages unless your use case requires advanced customization. Some packages include built-in support for Ollama, with preconfigured prompts for generating commit messages. One of such packages is Git AI Commit by the-cafe. Below is how you use Ollama with it.

Locally Generate Commit Messages with Git AI Commit by the-cafe

We've shown you how to set up the git-ai-commit python package in this section of the preceding article and demonstrated an example with the OpenAI API option.

To use Ollama, follow these instructions.

  1. Modify the tool configuration setup

    $ git-ai-commits config --setup
    

    You'll be asked to overwrite existing commit message hook configurations. Accept this override by pressing key y.

    git-ai-commit-ollama
  2. Select Ollama as your preferred AI provider and Mistral as your model choice.

    Choose your preferred AI provider:
    [?] Select AI Provider:
       OpenAI
       Anthropic
     > Ollama
    
    [?] Select Model: 
       ollama/llama3
     > ollama/mistral
       ollama/phi-3:medium
    
    [?] Enter your Ollama URL: http://localhost:11434/api/chat
    
  3. Stage file changes and generate commit messages with git-ai-commit summarize.

    $ git-ai-commit summarize
    
    Fetching your staged changes...
    
    Using Ollama url: http://localhost:11434/api/chat
    Using Ollama model: mistral
    Here is a summary of your changes:
    
       Added git-ai-commit-ollama and ollama-landing images
    
    to use this summary run: `git commit -m " Added git-ai-commit-ollama, ollama-landing i
    

That’s it.

More from this blog

T

TheTechEd Junction

5 posts

The TechEd Junction's goal is to educate: by providing well-researched, meticulously crafted guides, insights, and tutorials. Join us! Subscribe to our newsletter, and be the first to be in the know!