Git Change All Authors (Fast Version)
High-performance author standardization for large repositories
Metadata
- Author: Wei Guo
- Version: 1.0.0
- Dependencies: git 2.22+, git-filter-repo 2.26+, bash 4.0+, python3
This script rewrites ALL Git history! Remote URLs will be removed!
Code
bash
#!/bin/bash
#
# @title Git Change All Authors (Fast Version)
# @description High-performance author standardization for large repositories
# @author Wei Guo
# @version 1.0.0
# @license MIT
# @repository https://github.com/ropean/scripts
# @requires git 2.22+, git-filter-repo 2.26+, bash 4.0+, python3
# @note This script rewrites ALL Git history! Remote URLs will be removed!
#
# @example
# Usage:
# ./git-change-all-authors-fast.sh
#
################################################################################
# Git Change All Authors (Fast Version)
################################################################################
#
################################################################################
# DESCRIPTION
################################################################################
#
# This is the HIGH-PERFORMANCE version of git-change-all-authors.sh. It uses
# git-filter-repo instead of git filter-branch, providing 10-100x faster
# execution for large repositories.
#
# Key Features:
# - 10-100x faster than git-change-all-authors.sh
# - Modifies ALL commits in ALL branches
# - Optimized for large repositories (10,000+ commits)
# - Creates full repository backup
# - Memory efficient
#
# Use Cases:
# - Large repositories with thousands of commits
# - When git-change-all-authors.sh is too slow
# - Production repositories that need quick turnaround
# - Repositories with complex branch structures
#
################################################################################
# WHEN TO USE THIS SCRIPT
################################################################################
#
# ✅ Use git-change-all-authors-fast.sh when:
# - Repository has > 1,000 commits
# - git-change-all-authors.sh is too slow
# - You need fast execution time
# - You have git-filter-repo installed
#
# ❌ Don't use this script when:
# - You only need to fix a PR branch → Use git-change-pr-authors.sh
# - You only need to fix one commit → Use git-change-commit.sh
# - Repository is small (< 1,000 commits) → Use git-change-all-authors.sh
# - git-filter-repo is not available → Use git-change-all-authors.sh
#
################################################################################
# COMPARISON WITH OTHER SCRIPTS
################################################################################
#
# Script | Scope | Speed | Requirements
# ------------------------------|-------------|------------|---------------
# git-change-commit.sh | 1 commit | Fast | git only
# git-change-pr-authors.sh | Range | Fast | git only
# git-change-all-authors.sh | ALL commits | Slow | git only
# git-change-all-authors-fast.sh| ALL commits | Very Fast | git-filter-repo
#
# Performance Comparison (same repository):
# 1,000 commits: filter-branch: 1 min | filter-repo: 5 sec (12x faster)
# 10,000 commits: filter-branch: 20 min | filter-repo: 1 min (20x faster)
# 50,000 commits: filter-branch: 2 hours | filter-repo: 5 min (24x faster)
# 100,000 commits: filter-branch: 10 hours| filter-repo: 15 min (40x faster)
#
################################################################################
# INSTALLATION
################################################################################
#
# This script requires git-filter-repo. Install it first:
#
# macOS:
# brew install git-filter-repo
#
# Ubuntu/Debian:
# apt install git-filter-repo
#
# Fedora/RHEL:
# dnf install git-filter-repo
#
# Using pip:
# pip3 install git-filter-repo
#
# Manual:
# https://github.com/newren/git-filter-repo
#
################################################################################
# USAGE
################################################################################
#
# Syntax:
# ./git-change-all-authors-fast.sh
#
# No parameters needed - it processes all commits automatically.
#
# Examples:
# # Standard usage
# ./git-change-all-authors-fast.sh
#
# # The script will:
# # 1. Check if git-filter-repo is installed
# # 2. Show all current authors
# # 3. Ask for confirmation
# # 4. Create full repository backup
# # 5. Modify all commits (FAST!)
# # 6. Show results
#
################################################################################
# CONFIGURATION
################################################################################
#
# Before using, modify these variables:
# NEW_AUTHOR_NAME - The unified author name
# NEW_AUTHOR_EMAIL - The unified author email
#
# Note: This script sets both author and committer to the same values.
#
################################################################################
# IMPORTANT DIFFERENCES FROM STANDARD VERSION
################################################################################
#
# 1. Backup Strategy:
# - Standard: Creates backup branch
# - Fast: Creates full repository backup directory
#
# 2. Remote URLs:
# - Standard: Preserves remote URLs
# - Fast: Removes remote URLs (safety feature)
# - You must re-add remotes: git remote add origin <url>
#
# 3. Speed:
# - Standard: Processes ~10-50 commits/second
# - Fast: Processes ~1000-5000 commits/second
#
################################################################################
# PERFORMANCE NOTES
################################################################################
#
# Estimated execution time:
# 100 commits: < 1 second
# 1,000 commits: ~5 seconds
# 10,000 commits: ~1 minute
# 50,000 commits: ~5 minutes
# 100,000 commits:~15 minutes
#
# Memory usage:
# - Scales linearly with repository size
# - Typical: 100-500 MB for most repositories
# - Large repos (> 1GB): May need 1-2 GB RAM
#
################################################################################
set -e
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
NEW_AUTHOR_NAME="Wei Guo"
NEW_AUTHOR_EMAIL="wei.guo@moodys.com"
# Function to print colored messages
print_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_step() {
echo -e "${BLUE}[STEP]${NC} $1"
}
# Check if we're in a git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
print_error "Not a git repository"
exit 1
fi
# Check if git-filter-repo is installed
if ! command -v git-filter-repo &> /dev/null; then
print_error "git-filter-repo is not installed"
echo ""
echo "Install it with:"
echo " macOS: brew install git-filter-repo"
echo " Ubuntu: apt install git-filter-repo"
echo " pip: pip3 install git-filter-repo"
echo ""
echo "Or use the slower version: git-change-all-authors.sh"
exit 1
fi
# Check if there are uncommitted changes
if ! git diff-index --quiet HEAD -- 2>/dev/null; then
print_error "You have uncommitted changes. Please commit or stash them first."
exit 1
fi
# Get current branch
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
print_info "Current branch: $CURRENT_BRANCH"
# Show current authors
echo ""
print_step "Current authors in repository:"
git log --format="%an <%ae>" | sort -u | while read -r author; do
echo " - $author"
done
echo ""
print_warning "This will change ALL commits to:"
echo " Author & Committer: $NEW_AUTHOR_NAME <$NEW_AUTHOR_EMAIL>"
echo ""
print_warning "This operation will rewrite the entire Git history!"
print_warning "All commit hashes will change!"
echo ""
# Ask for confirmation
read -p "Are you sure you want to continue? (yes/no): " -r
echo
if [[ ! $REPLY =~ ^[Yy][Ee][Ss]$ ]]; then
print_info "Operation cancelled"
exit 0
fi
# Create backup
BACKUP_DIR="../$(basename $(pwd))-backup-$(date +%Y%m%d-%H%M%S)"
print_step "Creating backup at: $BACKUP_DIR"
cp -r . "$BACKUP_DIR"
print_info "Backup created at: $BACKUP_DIR"
# Create mailmap for filter-repo
MAILMAP_FILE=$(mktemp)
cat > "$MAILMAP_FILE" << EOF
$NEW_AUTHOR_NAME <$NEW_AUTHOR_EMAIL>
EOF
print_step "Rewriting Git history (using fast git-filter-repo)..."
# Use git-filter-repo to change all authors
git filter-repo --force --mailmap "$MAILMAP_FILE" --commit-callback "
commit.author_name = b'$NEW_AUTHOR_NAME'
commit.author_email = b'$NEW_AUTHOR_EMAIL'
commit.committer_name = b'$NEW_AUTHOR_NAME'
commit.committer_email = b'$NEW_AUTHOR_EMAIL'
"
# Clean up
rm -f "$MAILMAP_FILE"
echo ""
print_info "Successfully changed all authors!"
echo ""
print_step "Verification - Current authors in repository:"
git log --format="%an <%ae>" | sort -u | while read -r author; do
echo " - $author"
done
echo ""
print_warning "Next steps:"
echo " 1. Review the changes: git log --format=fuller"
echo " 2. Re-add remote: git remote add origin <url>"
echo " 3. Force push: git push --force-with-lease"
echo " 4. Backup location: $BACKUP_DIR"
echo ""
print_warning "Note: git-filter-repo removes remote URLs for safety"
echo " You need to re-add them manually"
echo ""
exit 0File Information
- Filename:
git-change-all-authors-fast.sh - Category: git
- Language: BASH