Skip to content

Instantly share code, notes, and snippets.

@scottwater
Created July 20, 2024 19:31
Show Gist options
  • Save scottwater/38f6da2cbbcd81a13bf17aa9bdc512cc to your computer and use it in GitHub Desktop.
Save scottwater/38f6da2cbbcd81a13bf17aa9bdc512cc to your computer and use it in GitHub Desktop.
Search Git for a string of text in recently deleted files (number of files is the second paramemter, defaults to 20)
#!/bin/bash
string_to_search=$1
num_commits=${2:-20}
if [ -z "$string_to_search" ]; then
echo "Usage: $0 <string_to_search> [<num_commits>]"
exit 1
fi
commits=$(git log -n $num_commits --format=%H)
for commit in $commits; do
deleted_files=$(git diff --name-only --diff-filter=D $commit^..$commit)
if [ -n "$deleted_files" ]; then
for file in $deleted_files; do
if git show $commit~:$file &> /dev/null; then
if git show $commit~:$file | grep -q "$string_to_search"; then
echo "Commit $commit: File $file contained '$string_to_search'"
fi
fi
done
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment