### Example: What You Type Source: https://github.com/f/git-rewrite-commits/blob/master/QUICK_START.md Example of commands to add a file and initiate a commit. ```bash git add src/auth.ts git commit ``` -------------------------------- ### Example: What You Get Source: https://github.com/f/git-rewrite-commits/blob/master/QUICK_START.md Example of an AI-generated commit message that appears in the editor. ```text feat(auth): implement JWT token validation middleware # ✨ AI-generated commit message above # Feel free to edit as needed before saving # # Files being committed: # M src/auth.ts ``` -------------------------------- ### Start Committing with AI Source: https://github.com/f/git-rewrite-commits/blob/master/QUICK_START.md Demonstrates the basic workflow of adding files and committing, triggering the AI message generation. ```bash git add . git commit # ✨ AI message appears automatically in your editor! ``` -------------------------------- ### Quick Start (No Installation Required) Source: https://github.com/f/git-rewrite-commits/blob/master/README.md Execute the tool directly using npx without global installation. ```bash npx git-rewrite-commits # or shorter: npx grec ``` -------------------------------- ### Troubleshooting: Hooks Not Working Source: https://github.com/f/git-rewrite-commits/blob/master/QUICK_START.md Commands to check if hooks are installed and to reinstall them. ```bash # Check if installed ls -la .git/hooks/ # Reinstall npx git-rewrite-commits --install-hooks ``` -------------------------------- ### Troubleshooting: API Key Not Set Source: https://github.com/f/git-rewrite-commits/blob/master/QUICK_START.md Command to set the API key and instructions to make it permanent. ```bash export OPENAI_API_KEY="sk-..." # Add to ~/.bashrc or ~/.zshrc to make permanent ``` -------------------------------- ### Uninstall Hooks Source: https://github.com/f/git-rewrite-commits/blob/master/QUICK_START.md Removes the installed git hooks. ```bash rm .git/hooks/prepare-commit-msg rm .git/hooks/post-commit rm .git/hooks/pre-push ``` -------------------------------- ### Troubleshooting: Want Different Template Source: https://github.com/f/git-rewrite-commits/blob/master/QUICK_START.md Commands to configure a custom commit template per repository or globally. ```bash # Per repository git config hooks.commitTemplate "πŸš€ feat: message" # Global (all repositories) git config --global hooks.commitTemplate "feat(scope): message" ``` -------------------------------- ### Installation Source: https://github.com/f/git-rewrite-commits/blob/master/docs/API.md Install the git-rewrite-commits package using npm. ```bash npm install git-rewrite-commits ``` -------------------------------- ### Install and Run Source: https://github.com/f/git-rewrite-commits/blob/master/docs/README.md Install and run the git-rewrite-commits tool using npx. ```bash npx git-rewrite-commits ``` -------------------------------- ### Set API Key Source: https://github.com/f/git-rewrite-commits/blob/master/QUICK_START.md Sets the OpenAI API key as an environment variable. ```bash export OPENAI_API_KEY="your-api-key-here" ``` -------------------------------- ### Set Commit Language Source: https://github.com/f/git-rewrite-commits/blob/master/QUICK_START.md Configures the language for AI-generated commit messages. ```bash git config hooks.commitLanguage "es" # Spanish git config hooks.commitLanguage "fr" # French git config hooks.commitLanguage "ja" # Japanese ``` -------------------------------- ### Set Team Commit Template Source: https://github.com/f/git-rewrite-commits/blob/master/QUICK_START.md Configures a custom commit message template for the current repository. ```bash git config hooks.commitTemplate "[JIRA-XXX] feat: message" ``` -------------------------------- ### Command Examples Source: https://github.com/f/git-rewrite-commits/blob/master/README.md Examples of using the git-rewrite-commits command with various options. ```bash # Using the full command name npx git-rewrite-commits [options] # Or using the short alias (grec) npx grec [options] # Rewrite entire git history npx git-rewrite-commits # Preview changes without applying (dry run) npx git-rewrite-commits --dry-run # Generate commit message for staged changes npx git-rewrite-commits --staged # Process only last 10 commits npx git-rewrite-commits --max-commits 10 # Use custom AI model npx git-rewrite-commits --model gpt-4 # Use local AI with Ollama npx git-rewrite-commits --provider ollama # Install/update git hooks npx git-rewrite-commits --install-hooks ``` -------------------------------- ### Global Installation Source: https://github.com/f/git-rewrite-commits/blob/master/README.md Install the tool globally for command-line access. ```bash # Full command name npm install -g git-rewrite-commits # Or install the short alias (grec = git-rewrite-commits) npm install -g grec # Both work identically: git-rewrite-commits --help grec --help # Same thing, just shorter! ``` -------------------------------- ### Installation Source: https://github.com/f/git-rewrite-commits/blob/master/packages/grec/README.md Install the grec command globally using npm. ```bash npm install -g grec ``` -------------------------------- ### OpenAI Provider Initialization Source: https://github.com/f/git-rewrite-commits/blob/master/docs/API.md Example of initializing GitCommitRewriter with the OpenAI provider. ```typescript const rewriter = new GitCommitRewriter({ provider: 'openai', apiKey: 'sk-...', // Your OpenAI API key model: 'gpt-4' // or 'gpt-3.5-turbo' }); ``` -------------------------------- ### Quick examples Source: https://github.com/f/git-rewrite-commits/blob/master/packages/grec/README.md Demonstrates common usage scenarios for the grec command. ```bash # Rewrite commit history with AI grec # Preview changes (dry run) grec --dry-run # Install git hooks grec --install-hooks # Generate message for staged changes grec --staged # Use local AI with Ollama grec --provider ollama ``` -------------------------------- ### Install dependencies Source: https://github.com/f/git-rewrite-commits/blob/master/CONTRIBUTING.md Command to install project dependencies using npm. ```bash npm install ``` -------------------------------- ### Manual Installation (Windows) Source: https://github.com/f/git-rewrite-commits/blob/master/hooks/README.md Manual steps to copy the pre-commit and prepare-commit-msg hooks on Windows. ```cmd copy hooks\pre-commit.bat .git\hooks\pre-commit copy hooks\prepare-commit-msg.bat .git\hooks\prepare-commit-msg ``` -------------------------------- ### Install Git Hooks Source: https://github.com/f/git-rewrite-commits/blob/master/docs/README.md Install the git hooks for git-rewrite-commits. ```bash npx git-rewrite-commits --install-hooks ``` -------------------------------- ### Real-World Examples: Automatic Commit Message Generation Source: https://github.com/f/git-rewrite-commits/blob/master/README.md Setup for automatic commit message generation using pre-commit and prepare-commit-msg hooks. ```bash # Install the hooks (using either command) npx git-rewrite-commits --install-hooks # or npx grec --install-hooks # Enable them (opt-in for security) git config hooks.preCommitPreview true # Preview before commit git config hooks.prepareCommitMsg true # Auto-generate in editor # Configure provider git config hooks.commitProvider ollama # or use OpenAI with OPENAI_API_KEY git config hooks.providerModel llama3.2 # Optional: specify model ``` -------------------------------- ### prepare-commit-msg Hook Installation Source: https://github.com/f/git-rewrite-commits/blob/master/hooks/README.md Instructions for manually installing the prepare-commit-msg hook on Unix-like systems. ```bash cp hooks/prepare-commit-msg .git/hooks/ chmod +x .git/hooks/prepare-commit-msg ``` -------------------------------- ### Manual Installation (Unix/macOS/Linux) Source: https://github.com/f/git-rewrite-commits/blob/master/hooks/README.md Manual steps to copy and make executable the pre-commit and prepare-commit-msg hooks on Unix-like systems. ```bash cp hooks/pre-commit .git/hooks/ cp hooks/prepare-commit-msg .git/hooks/ chmod +x .git/hooks/* ``` -------------------------------- ### Selective Rewriting Example Source: https://github.com/f/git-rewrite-commits/blob/master/docs/API.md Example of configuring the rewriter to process only the last 10 commits and not skip well-formed ones. ```typescript // Only rewrite the last 10 commits const rewriter = new GitCommitRewriter({ maxCommits: 10, skipWellFormed: false // Process all commits }); ``` -------------------------------- ### Install git-rewrite-commits Source: https://github.com/f/git-rewrite-commits/blob/master/hooks/README.md Command to install the git-rewrite-commits tool globally using npm or npx. ```bash npm install -g git-rewrite-commits # or use npx (no installation needed) ``` -------------------------------- ### Install git hooks Source: https://github.com/f/git-rewrite-commits/blob/master/README.md Install git hooks using the full command name or the short alias. ```bash npx git-rewrite-commits --install-hooks # or npx grec --install-hooks ``` -------------------------------- ### pre-commit Hook Installation Source: https://github.com/f/git-rewrite-commits/blob/master/hooks/README.md Instructions for manually installing the pre-commit hook on Unix-like systems. ```bash cp hooks/pre-commit .git/hooks/ chmod +x .git/hooks/pre-commit ``` -------------------------------- ### Example COMMIT_MESSAGE.md Source: https://github.com/f/git-rewrite-commits/blob/master/README.md Example of a COMMIT_MESSAGE.md file for project-specific commit message guidelines. ```markdown # Project Commit Guidelines ## Requirements - Use conventional commits with these scopes: auth, api, ui, db - Include ticket numbers when available (e.g., JIRA-123) - Security changes must be clearly marked - Breaking changes need BREAKING CHANGE in the message ## Project Context This is a financial services API that handles sensitive data. Emphasize security, compliance, and performance in commit messages. ``` -------------------------------- ### Basic Usage Source: https://github.com/f/git-rewrite-commits/blob/master/docs/API.md Example of initializing GitCommitRewriter and using its methods to rewrite history and generate commit messages for staged changes. ```typescript import { GitCommitRewriter } from 'git-rewrite-commits'; const rewriter = new GitCommitRewriter({ provider: 'openai', // or 'ollama' apiKey: process.env.OPENAI_API_KEY, model: 'gpt-4', // optional, defaults to gpt-3.5-turbo dryRun: false, verbose: true }); // Rewrite history await rewriter.rewriteHistory(); // Generate message for staged changes const message = await rewriter.generateForStaged(); console.log(message); ``` -------------------------------- ### generateForStaged Method Source: https://github.com/f/git-rewrite-commits/blob/master/docs/API.md Example of calling the generateForStaged method to get a commit message for staged changes. ```typescript const message = await rewriter.generateForStaged(); // Returns: "feat(auth): add JWT token validation" ``` -------------------------------- ### Real-World Examples: Manual Rewriting for Existing Commits Source: https://github.com/f/git-rewrite-commits/blob/master/README.md Example of cleaning up the last few commits before pushing. ```bash # Clean up the last 5 commits before pushing echo "πŸ”§ Improving commit messages before push..." npx git-rewrite-commits --max-commits 5 --dry-run echo "Apply changes? (y/n)" read answer if [ "$answer" = "y" ]; then ``` -------------------------------- ### Version Pinning in package.json Source: https://github.com/f/git-rewrite-commits/blob/master/SECURITY.md Example of how to pin the tool's version in the devDependencies section of package.json. ```json "devDependencies": { "git-rewrite-commits": "0.4.0" } ``` -------------------------------- ### Preparing for Open Source Source: https://github.com/f/git-rewrite-commits/blob/master/README.md Example of fixing all commits with a custom template, reviewing changes, and force pushing. ```bash # Fix all commits with custom template npx git-rewrite-commits \ --template "feat(scope): message" \ --language en \ --no-skip-well-formed # Review the changes git log --oneline -20 # If satisfied, force push git push --force-with-lease origin main ``` -------------------------------- ### Custom Prompt Usage Source: https://github.com/f/git-rewrite-commits/blob/master/docs/API.md Example of overriding AI behavior with a custom prompt. ```typescript const rewriter = new GitCommitRewriter({ prompt: 'Generate a commit message with emojis and enthusiasm' }); // Generates: "✨ feat(ui): Add amazing new dashboard! πŸš€" ``` -------------------------------- ### Message Replacement Example Source: https://github.com/f/git-rewrite-commits/blob/master/hooks/README.md Demonstrates how the AI commit message preview and replacement works, showing the user's input, the AI's suggested message, and the final result. ```bash # You type: git commit -m "blabla" # pre-commit shows: πŸ€– AI Commit Message Preview Suggested commit message: fix(hooks): resolve coordination issue between pre-commit and prepare-commit-msg This will REPLACE your commit message when you confirm. Continue with commit? (y/n) y # Result: Your "blabla" is replaced with the AI message! ``` -------------------------------- ### Multi-language Support Source: https://github.com/f/git-rewrite-commits/blob/master/docs/API.md Example of configuring the rewriter to generate commit messages in Spanish. ```typescript const rewriter = new GitCommitRewriter({ language: 'es' // Spanish }); // Generates: "feat(auth): aΓ±adir validaciΓ³n de token JWT" ``` -------------------------------- ### Basic Usage Source: https://github.com/f/git-rewrite-commits/blob/master/README.md Example of how to use git-rewrite-commits for basic improvements. ```bash npx git-rewrite-commits --dry-run --max-commits 10 npx git-rewrite-commits --max-commits 10 git push --force-with-lease origin feature-branch ``` -------------------------------- ### Template Usage Source: https://github.com/f/git-rewrite-commits/blob/master/docs/API.md Example of using a custom template to format commit messages. ```typescript const rewriter = new GitCommitRewriter({ template: '[JIRA-{ticket}] {type}: {description}' }); // Generates: "[JIRA-123] feat: add user authentication" ``` -------------------------------- ### Configure Ollama for local models Source: https://github.com/f/git-rewrite-commits/blob/master/hooks/README.md Commands to download a model and start the Ollama server for local AI model usage. ```bash # Install Ollama from https://ollama.ai ollama pull llama3.2 # Download the model ollama serve # Start Ollama server ``` -------------------------------- ### Conventional Commits Examples Source: https://github.com/f/git-rewrite-commits/blob/master/CONTRIBUTING.md Examples of commit messages following the Conventional Commits specification. ```bash feat: add support for GPT-4 vision model fix: handle empty commit messages properly docs: update installation instructions refactor: extract message generation logic test: add tests for backup creation chore: update dependencies ``` -------------------------------- ### Ollama Provider Initialization Source: https://github.com/f/git-rewrite-commits/blob/master/docs/API.md Example of initializing GitCommitRewriter with the Ollama provider for local model usage. ```typescript const rewriter = new GitCommitRewriter({ provider: 'ollama', model: 'llama3.2', // or any Ollama model ollamaUrl: 'http://localhost:11434' }); ``` -------------------------------- ### Very Quick Example Source: https://github.com/f/git-rewrite-commits/blob/master/README.md Demonstrates how to use the tool to rewrite a commit message for staged changes, assuming OPENAI_API_KEY is set. ```bash git add -A git commit -m "$(npx -y grec --quiet --staged --skip-remote-consent)" > [main 9979e7e] feat(specs): update subscription status handling in webhook controller 3 files changed, 432 insertions(+), 9 deletions(-) create mode 100644 spec/controllers/webhooks_spec.rb git push origin master ``` -------------------------------- ### rewriteHistory Method Source: https://github.com/f/git-rewrite-commits/blob/master/docs/API.md Example of calling the rewriteHistory method and logging the results. ```typescript const result = await rewriter.rewriteHistory(); console.log(`Improved ${result.improved} commits`); console.log(`Skipped ${result.skipped} well-formed commits`); ``` -------------------------------- ### Hooks Configuration via Environment Variables Source: https://github.com/f/git-rewrite-commits/blob/master/hooks/README.md Examples of environment variables for configuring the Git commit provider, model, template, and language. ```bash # Unix/macOS/Linux export GIT_COMMIT_PROVIDER="ollama" # or "openai" export GIT_COMMIT_MODEL="gpt-4" # or "llama3.2" export GIT_COMMIT_TEMPLATE="[JIRA-XXX] feat: message" export GIT_COMMIT_LANGUAGE="es" export OLLAMA_URL="http://192.168.1.100:11434" # Custom Ollama server URL ``` -------------------------------- ### Error Handling Source: https://github.com/f/git-rewrite-commits/blob/master/docs/API.md Example of a try-catch block to handle potential errors during commit rewriting. ```typescript try { await rewriter.rewriteHistory(); } catch (error) { if (error.message.includes('API key')) { console.error('Invalid API key'); } else if (error.message.includes('git repository')) { console.error('Not in a git repository'); } else { console.error('Error:', error.message); } } ``` -------------------------------- ### Using Pinned Version in Hooks Source: https://github.com/f/git-rewrite-commits/blob/master/SECURITY.md Example of how to use a specific pinned version of the tool within git hooks. ```bash npx git-rewrite-commits@0.4.0 --staged ``` -------------------------------- ### CI/CD Integration Source: https://github.com/f/git-rewrite-commits/blob/master/README.md Example of integrating git-rewrite-commits into a CI pipeline for Pull Request validation. ```yaml - name: Check Commit Quality run: | npx git-rewrite-commits --dry-run --max-commits ${{ github.event.pull_request.commits }} # This will show which commits would be improved ``` -------------------------------- ### Committing Hooks to the Repo Source: https://github.com/f/git-rewrite-commits/blob/master/hooks/README.md Steps to commit Git hooks to a repository and have team members install them. ```bash # Create a .githooks directory mkdir .githooks cp hooks/* .githooks/ # Team members can then install with: git config core.hooksPath .githooks git config hooks.preCommitPreview true # Enable preview git config hooks.prepareCommitMsg true # Enable generation ``` -------------------------------- ### Test on a sample repository Source: https://github.com/f/git-rewrite-commits/blob/master/CONTRIBUTING.md Steps to set up a test repository and run the tool in dry-run mode. ```bash # Create a test repo mkdir test-repo && cd test-repo git init # Create some commits echo "test" > file.txt && git add . && git commit -m "bad message 1" echo "test2" > file2.txt && git add . && git commit -m "bad message 2" # Run the tool in dry-run mode npx .. --dry-run --api-key "your-api-key" ``` -------------------------------- ### Custom Provider Implementations Source: https://github.com/f/git-rewrite-commits/blob/master/docs/API.md Demonstrates how to create and use a custom AI provider, specifically an Ollama provider with the 'codellama' model, using environment variables for configuration. ```typescript import { createProvider } from 'git-rewrite-commits/providers'; const customProvider = createProvider({ provider: 'ollama', model: 'codellama', ollamaUrl: process.env.OLLAMA_URL }); ``` -------------------------------- ### Build the project Source: https://github.com/f/git-rewrite-commits/blob/master/CONTRIBUTING.md Command to build the project using npm. ```bash npm run build ``` -------------------------------- ### Enable Git Hooks Source: https://github.com/f/git-rewrite-commits/blob/master/docs/SECURITY.md Explicitly enable git hooks for security. ```bash # Hooks must be explicitly enabled git config hooks.preCommitPreview true # Enable preview hook git config hooks.prepareCommitMsg true # Enable generation hook ``` -------------------------------- ### Complete usage Source: https://github.com/f/git-rewrite-commits/blob/master/packages/grec/README.md Lists all available options for the grec command, which are compatible with git-rewrite-commits. ```bash # All options from git-rewrite-commits work: grec [options] Options: -h, --help Show help -V, --version Show version -d, --dry-run Preview changes without applying -v, --verbose Show detailed output -b, --branch Target branch (default: current) -m, --max-commits Process only last n commits -s, --staged Generate message for staged changes --skip-backup Don't create backup branch --skip-well-formed Skip already well-formed commits (default) --no-skip-well-formed Process all commits --min-quality Min quality score (0-10, default: 7) --provider AI provider: openai/ollama (default: openai) --model AI model to use --template