Reference library

Git + Python

Automate Git from Python — diffs, hooks, release tags, and repo housekeeping.

4 matches
Git + Python easy

Git Signing in Python

Sign and verify Git commits with a mock GPG implementation using HMAC and SHA-256.

git signing hmac
Python
import hashlib
import hmac

class GPGMock:
    def __init__(self, secret_key):
        self.secret_key = secret_key.encode()

    def sign_commit(self, commit_message):
        """Mock GPG signing by computing an HMAC of the commit message."""
        signature = hmac.new(self.secret_key, commit_message.encode(), hash…
13 0 Open
Git + Python easy

How to Filter Git History to Remove Secret File Entries in Python

A pure-Python mock that filters a repository's history to drop any commit that touched a secret file, so you can plan a cleanup before rewriting Git history.

git secrets history
Python
from pathlib import Path
import json

def filter_history(history, secret_path):
    """Remove entries that touch the secret file."""
    return [entry for entry in history if secret_path not in entry["files"]]

if __name__ == "__main__":
    repo_history = [
        {"commit": "a1b2c3", "message": "Add app", "files": …
10 0 Open
Git + Python easy

How to detect secrets in git history with Python

Scan a git history export file for common secret patterns using regex and Python.

git secrets security
Python
import re
from pathlib import Path


def scan_history_for_secrets(history_file: str) -> list:
    """Scan a git history export for potential secrets using regex patterns."""
    patterns = {
        "AWS Access Key": r"AKIA[0-9A-Z]{16}",
        "GitHub Token": r"gh[pousr]_[0-9A-Za-z]{36,255}",
        "Private Key": …
12 0 Open
Git + Python easy

Verify Git tag signatures with HMAC in Python

Create and verify deterministic HMAC-SHA256 signatures for git tags using the Python standard library.

hmac git security
Python
import hashlib
import hmac


def sign_tag(tag: str, secret_key: str) -> str:
    """Create a deterministic HMAC signature for a tag."""
    message = tag.encode("utf-8")
    key = secret_key.encode("utf-8")
    return hmac.new(key, message, hashlib.sha256).hexdigest()


def verify_signed_tag(tag: str, signature: str, …
14 0 Open

Browse by section

Each section groups closely related Python snippets.

Git + Python — Python code examples

What you will find here

This page collects git + python snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.