Skip to content

Instantly share code, notes, and snippets.

@cheshire137
Last active June 24, 2016 16:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cheshire137/8b40b9a9ddd281b0fde556ef1346cc9a to your computer and use it in GitHub Desktop.
Save cheshire137/8b40b9a9ddd281b0fde556ef1346cc9a to your computer and use it in GitHub Desktop.
Find all the Git commits that modified a particular line in a file.
#!/bin/bash
# Sample use:
# PATTERN="\"/some-route\"" FILE=config/routes.rb ~/bin/git-line-history.sh
# Thanks to mikedillion <https://github.com/mikedillion> for the basis of this!
set -e
#PATTERN="version':"
#FILE="setup.py"
if [ -z "$REF" ]; then
REF=HEAD
echo "Starting with $REF"
fi
if [ -z "$FILE" ]; then
echo "Pass FILE environment variable"
exit 1
fi
if [ -z "$PATTERN" ]; then
echo "Pass PATTERN environment variable"
exit 1
fi
if [ -z "$REPO_URL" ]; then
REPO_URL="https://github.com/your-favorite/default-repo"
echo "Assuming REPO_URL should be $REPO_URL"
fi
while true; do
BLAME_LINE=$(git blame $REF^ $FILE | grep "$PATTERN")
NEW_REF=$(echo $BLAME_LINE | awk '{print $1}')
echo "$NEW_REF - $REPO_URL/commit/$NEW_REF"
REF=$NEW_REF
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment