Back to Code Bytes
3 min read
git-churn

Description

git-churn is a simple yet powerful script that wraps git log to provide insights into which files change the most frequently. This is useful for finding code “hotspots” - files that are modified often and may need refactoring, better testing, or architectural improvements. The script counts how many times each file has been modified across all commits and presents the results in a sorted table format. Files with high churn rates often indicate areas of technical debt or rapidly evolving features that might benefit from additional attention.

Code Byte

#!/bin/bash
#
# Written by Corey Haines
# Scriptified by Gary Bernhardt
#
# Put this anywhere on your $PATH (~/bin is recommended). Then git will see it
# and you'll be able to do `git churn`.
#
# Show churn for whole repo:
#   $ git churn
#
# Show churn for specific directories:
#   $ git churn app lib
#
# Show churn for a time range:
#   $ git churn --since='1 month ago'
#
# (These are all standard arguments to `git log`.)

set -e
git log --all -M -C --name-only --format='format:' "$@" | sort | grep -v '^$' | uniq -c | sort | awk 'BEGIN {print "count\tfile"} {print $1 "\t" $2}' | sort -g

source: git-churn

Setup:

# Create the directory if it doesn't exist
mkdir -p ~/bin


# Save the script as git-churn (no .sh extension needed)
nano ~/bin/git-churn
# (paste the script content from above)

# Make it executable
chmod +x ~/bin/git-churn

# Add ~/bin to your PATH if it's not already there
echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bashrc
# or for zsh users:
echo 'export PATH="$HOME/bin:$PATH"' >> ~/.zshrc

# Reload your shell
source ~/.bashrc
# or
source ~/.zshrc

Check if it’s found

which git-churn

Example usage:

# Show churn for entire repository
git churn

# Show top 10 most changed files in the last 6 months for app directory
git churn --since='6 months ago' app/ | tail

# Show churn for files changed in the last month
git churn --since='1 month ago'

# Show churn for specific directories
git churn src/ tests/

# Show churn for files by a specific author
git churn --author="John Doe"