rerere Git Command Guide
The git rerere (reuse recorded resolution) command records conflict resolutions and automatically applies them when the same conflicts occur again. This is particularly useful for long-lived topic branches that are frequently rebased or merged.
git rerere Syntax:
Section titled “git rerere Syntax:”git rerere [clear | forget <pathspec>... | diff | status | remaining | gc]Commands:
Section titled “Commands:”| Command | Description |
|---|---|
| (no command) | Automatically record resolution during conflicts |
clear | Reset metadata if merge resolution is aborted |
forget <pathspec> | Forget recorded resolutions for specified paths |
diff | Show diffs for current conflict resolutions |
status | Show paths with recorded resolutions |
remaining | Show paths with conflicts that can be resolved |
gc | Prune old recorded resolutions |
Understanding Rerere:
Section titled “Understanding Rerere:”How Rerere Works:
Section titled “How Rerere Works:”Rerere Workflow:1. Enable rerere with git config rerere.enabled true2. During merge conflict, resolve conflicts manually3. Git automatically records the resolution4. Next time same conflict occurs, resolution is applied automatically5. Use git rerere to manage recorded resolutions
Storage Location:├── Recorded resolutions stored in .git/rr-cache/├── Each conflict gets SHA-1 based directory├── Preimage and postimage stored for each resolution└── Metadata tracks resolution stateConflict Resolution Recording:
Section titled “Conflict Resolution Recording:”Recording Process:├── Git detects merge conflict├── Creates preimage (original conflicted state)├── User resolves conflicts manually├── Git creates postimage (resolved state)├── SHA-1 of preimage becomes storage key└── Resolution stored for future reuse
Automatic Application:├── Git encounters same conflict again├── Computes SHA-1 of current conflict state├── Looks up stored resolution├── Applies resolution automatically└── User sees clean merge resultWhen Rerere Helps:
Section titled “When Rerere Helps:”Ideal Use Cases:├── Long-lived feature branches├── Frequent rebasing of topic branches├── Repeated merges with same conflicts├── Collaborative development with merge conflicts└── Automated conflict resolution in CI/CD
Not Helpful For:├── One-time merges├── Completely different conflicts├── Conflicts requiring different resolutions└── Very large repositories (storage overhead)Basic Rerere Operations:
Section titled “Basic Rerere Operations:”Enabling and Using Rerere:
Section titled “Enabling and Using Rerere:”# Enable rerere globallygit config rerere.enabled true
# Enable for current repositorygit config rerere.enabled true
# Check if enabledgit config rerere.enabled
# First time setup with conflict resolutiongit merge feature-branch# Resolve conflicts manuallygit add resolved-file.txtgit commit
# Next time same conflict occursgit merge feature-branch # Resolution applied automaticallyManaging Recorded Resolutions:
Section titled “Managing Recorded Resolutions:”# View current statusgit rerere status
# Show remaining conflicts that can be resolvedgit rerere remaining
# Show diffs of recorded resolutionsgit rerere diff
# Forget resolution for specific pathgit rerere forget path/to/file.txt
# Clear all recorded resolutionsgit rerere clearGarbage Collection:
Section titled “Garbage Collection:”# Clean up old resolutionsgit rerere gc
# Manual cleanup of rr-cachefind .git/rr-cache -type f -mtime +90 -delete
# Check cache sizedu -sh .git/rr-cache/Advanced Rerere Scenarios:
Section titled “Advanced Rerere Scenarios:”Long-Lived Branch Workflow:
Section titled “Long-Lived Branch Workflow:”# Set up rerere for long-lived branchsetup_long_lived_branch() { local branch_name="$1"
echo "Setting up rerere for long-lived branch: $branch_name"
# Enable rerere git config rerere.enabled true
# Create and switch to branch git checkout -b "$branch_name"
# Initial setup complete echo "Rerere enabled. Conflict resolutions will be recorded automatically."}
# Usage examplesetup_long_lived_branch "feature/complex-ui"
# As you work and rebase frequently:# 1. Conflicts occur during rebase# 2. Resolve them once# 3. Future rebases apply resolutions automaticallyCollaborative Development:
Section titled “Collaborative Development:”# Share rerere resolutions in teamshare_rerere_resolutions() { echo "Setting up shared rerere resolutions"
# Configure shared rerere directory git config rerere.enabled true
# For team collaboration, consider: # - Sharing rr-cache via network storage # - Including resolutions in repository # - Documenting resolution strategies
echo "Rerere configured for collaborative use"}
# Team workflow:# 1. Team enables rerere# 2. First person resolves conflicts# 3. Others benefit from recorded resolutions# 4. Share particularly complex resolutionsAutomated Testing Integration:
Section titled “Automated Testing Integration:”# Use rerere in CI/CD for testingci_rerere_integration() { echo "Setting up rerere for CI/CD testing"
# Enable rerere in CI git config rerere.enabled true
# Pre-populate common resolutions # (Could be done by copying rr-cache from previous runs)
# Run tests with rerere if git merge test-branch; then echo "✓ Merge successful (possibly using rerere)" run-tests.sh else echo "✗ Merge failed" exit 1 fi}
ci_rerere_integrationConfiguration and Best Practices:
Section titled “Configuration and Best Practices:”Git Configuration for Rerere:
Section titled “Git Configuration for Rerere:”# Enable rereregit config rerere.enabled true
# Configure automatic resolutiongit config rerere.autoUpdate true # Stage resolved files
# Configure storagegit config rerere.enabled true # Basic enablegit config gc.rerereResolved 60 # Days to keep resolutionsgit config gc.rerereUnresolved 15 # Days to keep unresolved
# Configure for large teamsgit config rerere.enabled truegit config rerere.share true # Share resolutions (experimental)Safe Rerere Usage:
Section titled “Safe Rerere Usage:”# Always review auto-applied resolutionsgit rerere status # Check what was appliedgit diff # Verify the results
# Backup before complex operationscp -r .git/rr-cache .git/rr-cache.backup
# Test rerere behaviorgit config rerere.enabled true# Perform test merge with conflicts# Verify behavior
# Monitor rr-cache sizewatch -n 60 'du -sh .git/rr-cache'Rerere Strategy Guidelines:
Section titled “Rerere Strategy Guidelines:”# When to use rerere:# - Long-lived branches with frequent rebases# - Teams with recurring merge conflicts# - Automated processes with predictable conflicts# - Development workflows with repeated merges
# When to avoid rerere:# - Short-lived branches# - One-time merges# - Conflicts requiring different resolutions# - Security-sensitive code (review all resolutions)
# Best practices:# - Enable rerere early in project# - Review auto-applied resolutions# - Clean up old resolutions regularly# - Document complex resolution strategiesIntegration with Development Workflows:
Section titled “Integration with Development Workflows:”Rebase Workflow with Rerere:
Section titled “Rebase Workflow with Rerere:”#!/bin/bash# Rebase workflow enhanced with rerere
rerere_rebase_workflow() { local target_branch="${1:-main}"
echo "Rebasing with rerere support onto $target_branch"
# Ensure rerere is enabled if [ "$(git config rerere.enabled)" != "true" ]; then echo "Enabling rerere for better rebase experience" git config rerere.enabled true fi
# Fetch latest changes git fetch origin "$target_branch"
# Attempt rebase if git rebase "origin/$target_branch"; then echo "✓ Rebase completed successfully"
# Check if rerere helped if git rerere status | grep -q .; then echo "Rerere applied resolutions for:" git rerere status fi
else echo "Rebase encountered conflicts"
# Show remaining conflicts git rerere remaining
# Guide user through resolution echo "Resolve conflicts, then run:" echo " git add <resolved-files>" echo " git rebase --continue" echo "" echo "Rerere will record your resolutions for next time" fi}
rerere_rebase_workflow "main"Conflict Resolution Training:
Section titled “Conflict Resolution Training:”# Use rerere to train conflict resolutiontrain_conflict_resolution() { echo "Training conflict resolution with rerere"
# Create test scenario git checkout -b training # Make conflicting changes echo "conflicting change 1" > conflict.txt git add conflict.txt git commit -m "Add conflicting change 1"
git checkout main echo "conflicting change 2" > conflict.txt git add conflict.txt git commit -m "Add conflicting change 2"
# Enable rerere git config rerere.enabled true
# Perform merge to create conflict if ! git merge training 2>/dev/null; then echo "Conflict created. Resolve it:" echo "Edit conflict.txt, then:" echo " git add conflict.txt" echo " git commit" echo "" echo "Rerere will remember this resolution" fi}
train_conflict_resolutionRepository Maintenance:
Section titled “Repository Maintenance:”# Maintain rerere databasererere_maintenance() { echo "Performing rerere maintenance"
# Check rerere status if [ "$(git config rerere.enabled)" = "true" ]; then echo "Rerere is enabled"
# Show current status echo "Recorded resolutions:" git rerere status
# Clean up old resolutions git rerere gc
# Show cache statistics echo "Cache statistics:" find .git/rr-cache -type f | wc -l du -sh .git/rr-cache 2>/dev/null || echo "No rr-cache directory"
else echo "Rerere is not enabled" read -p "Enable rerere? (y/N): " enable if [[ "$enable" == "y" ]]; then git config rerere.enabled true echo "Rerere enabled" fi fi}
rerere_maintenanceTroubleshooting Common Issues:
Section titled “Troubleshooting Common Issues:”Rerere Not Working:
Section titled “Rerere Not Working:”# Debug rerere issuesdebug_rerere() { echo "Debugging rerere issues"
# Check if enabled if [ "$(git config rerere.enabled)" != "true" ]; then echo "Rerere is not enabled" git config rerere.enabled true return fi
# Check rr-cache directory if [ ! -d .git/rr-cache ]; then echo "rr-cache directory does not exist" mkdir -p .git/rr-cache fi
# Check permissions if [ ! -w .git/rr-cache ]; then echo "rr-cache directory not writable" chmod 755 .git/rr-cache fi
# Test with simple conflict echo "Testing rerere with simple conflict..." # Create test scenario}
debug_rerereIncorrect Resolutions:
Section titled “Incorrect Resolutions:”# Handle incorrect auto-applied resolutionsfix_incorrect_resolution() { echo "Fixing incorrect rerere resolution"
# Check what was applied git rerere status
# Undo the auto-resolution git checkout HEAD -- <auto-resolved-file>
# Resolve conflict manually # Edit file to fix conflict properly git add <file>
# Forget the incorrect resolution git rerere forget <file>
# Continue with merge/rebase git commit # or git rebase --continue}
fix_incorrect_resolutionStorage Issues:
Section titled “Storage Issues:”# Manage rerere storagemanage_rerere_storage() { echo "Managing rerere storage"
# Check storage usage du -sh .git/rr-cache/
# List all recorded resolutions find .git/rr-cache -name "postimage" | while read -r file; do dir=$(dirname "$file") echo "Resolution: $(basename "$dir")" done
# Clean up old resolutions find .git/rr-cache -type f -mtime +90 -delete
# Reorganize storage git rerere gc}
manage_rerere_storagePerformance Issues:
Section titled “Performance Issues:”# Optimize rerere performanceoptimize_rerere_performance() { echo "Optimizing rerere performance"
# Limit cache size git config gc.rerereResolved 30 # Keep less time
# Clean up frequently git rerere gc
# Use faster storage if possible # (Consider moving rr-cache to faster disk)
# Monitor performance time git rerere status}
optimize_rerere_performanceSharing Issues:
Section titled “Sharing Issues:”# Handle rerere in shared repositorieshandle_shared_rerere() { echo "Handling rerere in shared repositories"
# Check if sharing is desired if [ -d .git/rr-cache ]; then echo "Local rerere cache exists"
# Option 1: Keep local echo "Keeping rerere local to developer"
# Option 2: Share via repository # cp -r .git/rr-cache .git-shared/ # git add .git-shared # git commit -m "Share rerere resolutions"
# Option 3: Share via network storage # mount network storage to .git/rr-cache fi
echo "Shared rerere setup complete"}
handle_shared_rerereReal-World Usage Examples:
Section titled “Real-World Usage Examples:”Long-Term Feature Development:
Section titled “Long-Term Feature Development:”#!/bin/bash# Long-term feature development with rerere
long_term_feature_workflow() { local feature_name="$1"
echo "Setting up long-term feature development: $feature_name"
# Enable rerere early git config rerere.enabled true
# Create feature branch git checkout -b "feature/$feature_name"
# Document rerere benefits cat << 'EOF'Rerere Benefits for Long-Term Features:
1. Conflict Resolution Learning: - First conflict resolution is recorded - Future similar conflicts resolve automatically - Reduces repetitive work
2. Rebase-Friendly Development: - Frequent rebases become easier - Conflict resolution time decreases - More confident rebasing
3. Team Collaboration: - Share resolution strategies - Consistent conflict handling - Faster integration
Usage Tips:- Enable rerere at branch start- Review auto-applied resolutions- Clean up when feature completes- Share complex resolutions with teamEOF
echo "Long-term feature workflow configured" echo "Rerere will help with recurring conflicts"}
long_term_feature_workflow "user-authentication-system"Open Source Maintenance:
Section titled “Open Source Maintenance:”# Rerere for open source maintenanceopen_source_maintenance() { echo "Setting up rerere for open source maintenance"
# Enable rerere for upstream work git config rerere.enabled true
# Configure for frequent rebasing git config rerere.autoUpdate true
# Set up workflow cat << 'EOF'Open Source Maintenance with Rerere:
1. Fork Management: - Keep fork updated with upstream - Frequent rebases cause conflicts - Rerere remembers resolutions
2. Patch Series Maintenance: - Maintain multiple patches - Rebase frequently for updates - Rerere handles recurring conflicts
3. Review Integration: - Apply review feedback - Rebase and re-submit - Rerere maintains resolutions
Workflow: git checkout feature-branch git rebase upstream/main # Rerere helps here # Resolve any new conflicts # Rerere records for next time git push origin feature-branchEOF
echo "Open source maintenance workflow configured"}
open_source_maintenanceCI/CD Pipeline Optimization:
Section titled “CI/CD Pipeline Optimization:”# Optimize CI/CD with rerereci_rerere_optimization() { echo "Optimizing CI/CD pipelines with rerere"
# Enable rerere in CI git config rerere.enabled true
# Pre-populate common resolutions if [ -d ci-rerere-cache ]; then cp -r ci-rerere-cache .git/rr-cache fi
# Run CI process echo "Running CI with rerere optimization..."
# Example: Automated merging if git merge --no-edit automated-branch 2>/dev/null; then echo "✓ Merge successful with rerere" else echo "Merge had conflicts - resolving..."
# Apply any available rerere resolutions git rerere remaining | while read -r file; do if git rerere diff "$file" >/dev/null 2>&1; then echo "Auto-resolving $file with rerere" # Resolution already applied fi done
# Check if all conflicts resolved if git diff --quiet; then git commit --no-edit echo "✓ Conflicts resolved with rerere" else echo "✗ Manual conflict resolution required" exit 1 fi fi
# Save updated cache for future runs cp -r .git/rr-cache ci-rerere-cache/
echo "CI/CD optimization complete"}
ci_rerere_optimizationEducational Training:
Section titled “Educational Training:”# Use rerere for conflict resolution trainingconflict_resolution_training() { echo "Setting up conflict resolution training with rerere"
# Create training repository mkdir training-repo cd training-repo git init
# Enable rerere git config rerere.enabled true
# Create base content echo "base content" > file.txt git add file.txt git commit -m "Initial commit"
# Create conflicting branches git checkout -b branch-a echo "content from branch A" >> file.txt git commit -am "Add branch A content"
git checkout main git checkout -b branch-b echo "content from branch B" >> file.txt git commit -am "Add branch B content"
# Document training exercise cat << 'EOF'Conflict Resolution Training Exercise:
1. Try to merge branch-a into branch-b: git checkout branch-b git merge branch-a
2. Resolve the conflict manually - Edit file.txt to include both changes - git add file.txt - git commit
3. Rerere has now recorded the resolution
4. Try merging again in different scenario: git checkout -b test-merge git merge branch-a # Should resolve automatically
5. Verify rerere worked: git rerere status git rerere diff
Learning Points:- Rerere remembers exact conflict resolutions- Same conflicts resolve automatically- Different conflicts still need manual resolution- Review auto-applied resolutionsEOF
echo "Training setup complete" echo "Follow the exercise in the documentation above"}
conflict_resolution_trainingRepository Migration Aid:
Section titled “Repository Migration Aid:”# Use rerere during repository migrationmigration_rerere_support() { local source_repo="$1" local target_repo="$2"
echo "Using rerere to aid repository migration"
# Enable rerere git config rerere.enabled true
# During migration, conflicts may occur # Rerere can help resolve recurring conflicts
echo "Migration rerere support configured" echo "Rerere will help with any recurring conflicts during migration"
# Example migration workflow: cat << 'EOF'Migration with Rerere Support:
1. Start migration process2. Enable rerere before first conflicts3. Resolve conflicts manually first time4. Rerere records resolutions5. Subsequent similar conflicts resolve automatically6. Review all auto-applied resolutions7. Complete migration with confidenceEOF}
migration_rerere_support "/path/to/source" "/path/to/target"What’s the difference between rerere and manual conflict resolution?
Section titled “What’s the difference between rerere and manual conflict resolution?”Rerere automatically applies previously recorded conflict resolutions when the same conflicts occur again, while manual resolution requires resolving each conflict instance individually.
How do I enable rerere?
Section titled “How do I enable rerere?”Use git config rerere.enabled true to enable rerere globally or for the current repository.
Can rerere resolve all types of conflicts?
Section titled “Can rerere resolve all types of conflicts?”No, rerere only resolves conflicts that have identical pre-conflict states to previously recorded resolutions. Different conflicts still need manual resolution.
How do I see what rerere has recorded?
Section titled “How do I see what rerere has recorded?”Use git rerere status to see current recordings, git rerere diff to see the recorded resolutions, and git rerere remaining to see conflicts that can be auto-resolved.
Is rerere safe to use?
Section titled “Is rerere safe to use?”Rerere is generally safe but can apply incorrect resolutions if conflicts are similar but not identical. Always review auto-applied resolutions, especially for critical code.
How do I disable rerere for a specific operation?
Section titled “How do I disable rerere for a specific operation?”You can temporarily disable with git -c rerere.enabled=false
Can rerere work across different repositories?
Section titled “Can rerere work across different repositories?”Rerere resolutions are local to each repository, but you can copy .git/rr-cache directories between repositories to share resolutions.
How do I clean up old rerere recordings?
Section titled “How do I clean up old rerere recordings?”Use git rerere gc to clean up old recordings, or manually delete old files from .git/rr-cache.
Does rerere affect repository size?
Section titled “Does rerere affect repository size?”Yes, rerere stores conflict resolutions in .git/rr-cache, which can grow over time. Regular cleanup with gc helps manage size.
Can rerere be used in automated scripts?
Section titled “Can rerere be used in automated scripts?”Yes, but with caution. Ensure the script reviews auto-applied resolutions and handles cases where conflicts differ from recorded resolutions.
How do I share rerere resolutions with my team?
Section titled “How do I share rerere resolutions with my team?”Copy .git/rr-cache directories, commit them to the repository, or use shared network storage for the rr-cache directory.
What’s the difference between rerere and gitattributes conflict markers?
Section titled “What’s the difference between rerere and gitattributes conflict markers?”Rerere records complete conflict resolutions, while gitattributes can only specify conflict marker styles. Rerere provides actual resolution reuse.
Can rerere handle binary file conflicts?
Section titled “Can rerere handle binary file conflicts?”No, rerere only works with text files where conflicts can be detected and resolved using diff/patch mechanisms.
How do I forget a specific rerere resolution?
Section titled “How do I forget a specific rerere resolution?”Use git rerere forget
Does rerere work with all merge tools?
Section titled “Does rerere work with all merge tools?”Yes, rerere works independently of merge tools. It records the final resolved state regardless of how conflicts were resolved.
Can rerere be used with rebase operations?
Section titled “Can rerere be used with rebase operations?”Yes, rerere works with rebase, merge, and other operations that create conflicts. It’s particularly useful for frequent rebasing.
Applications of the git rerere command
Section titled “Applications of the git rerere command”- Long-Lived Branch Management: Automatically resolve recurring conflicts in long-term feature branches
- Frequent Rebase Workflows: Reduce conflict resolution time during frequent rebasing
- Team Collaboration: Share conflict resolution strategies across team members
- CI/CD Optimization: Speed up automated merge processes with recorded resolutions
- Conflict Resolution Training: Learn and document conflict resolution patterns
- Repository Maintenance: Maintain consistent conflict resolution approaches