Post-Merge Git Hook: Summarizing Changes with OpenAI

Posted on in programming

Merges often bring in massive changes — sometimes dozens of commits and hundreds of lines of code — and the first thing developers ask is: “What just happened?”

Wouldn’t it be great if Git could summarize what a merge brought in, in plain English, right after you run git pull or git merge?

In this article, we’ll build a post-merge Git hook powered by OpenAI that automatically summarizes the changes from the most recent merge. You’ll see how to capture the diff intelligently, generate a concise summary, and optionally notify your team via Slack or email.

Why a Post-Merge Hook?

The post-merge hook runs every time Git completes a successful merge or pull. This is the perfect time to:

  • Explain the changes that just landed
  • Detect and flag unexpected modifications
  • Send a summary to teammates asynchronously

It’s especially useful for:

  • Long-lived feature branches
  • Teams working across time zones
  • Infrastructure and monorepos with complex merge patterns

What We’ll Build

Our hook will:

  1. Detect the last merge commit
  2. Diff it against its parents
  3. Send the changes to OpenAI
  4. Print and optionally save or send a summary

We’ll add this as a command in our existing CLI tool, and wire it into .git/hooks/post-merge.

Step 1: Detect the Merge Commit

In merge_summary.py:

from git import Repo

def get_last_merge_commit(repo_path="."):
    repo = Repo(repo_path)
    for commit in repo.iter_commits('HEAD', max_count=20):
        if len(commit.parents) > 1:
            return commit
    return None

def get_merge_diff(commit):
    parent_a, parent_b = commit.parents[0], commit.parents[1]
    diff = parent_b.diff(parent_a, create_patch=True)
    return "\n".join(
        d.diff.decode('utf-8', errors='ignore') for d in diff if d.diff
    )

Step 2: Generate the Summary

In openai_utils.py (reuse from previous projects):

def summarize_merge(diff: str) -> str:
    prompt = f"""
You're an engineering lead. Summarize this Git diff from a merge commit. Use 
clear language. Highlight key changes, fixes, or new features.

{diff}
"""
    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}]
    )
    return response['choices'][0]['message']['content'].strip()

Step 3: Add to CLI

In cli.py:

from ai_git_hooks.merge_summary import get_last_merge_commit, get_merge_diff
from ai_git_hooks.openai_utils import summarize_merge

@cli.command()
def post_merge():
    """Summarize the most recent merge commit"""
    commit = get_last_merge_commit()
    if not commit:
        click.echo("No recent merge commit found.")
        return

    diff = get_merge_diff(commit)
    summary = summarize_merge(diff)

    click.echo("\n--- Merge Summary ---")
    click.echo(summary)

    with open("MERGE_SUMMARY.txt", "w") as f:
        f.write(summary + "\n")

Run it manually:

ai-git-hooks post-merge

Step 4: Install the Hook

In .git/hooks/post-merge:

#!/bin/sh
ai-git-hooks post-merge

Make it executable:

chmod +x .git/hooks/post-merge

Now, every time you merge or pull, you’ll get an immediate AI summary of what changed.

Optional: Notify Your Team

You can integrate with Slack or email using services like:

Example with Slack:

import requests

def notify_slack(summary, webhook_url):
    payload = {"text": f"*Merge Summary:*\n{summary}"}
    requests.post(webhook_url, json=payload)

Then add to the CLI command if SLACK_WEBHOOK_URL is set.

Use Cases and Extensions

  • Print summaries to terminal or log to a file
  • Email summaries to repo stakeholders
  • Trigger post-merge AI-assisted code review
  • Include ticket references by scanning commit messages

Conclusion

Your merge shouldn't be a mystery. With a simple Git hook and a touch of OpenAI, you can generate immediate, contextual summaries of code changes — reducing cognitive overhead, onboarding friction, and miscommunication.

Next up in our Git hook series: the AI-powered pre-push policy validator, built to scan for secrets, insecure patterns, or team-specific guardrails.

Want to explore more Git+AI automation? Check out our tools and ideas at Slaptijack

Slaptijack's Koding Kraken