Skip to content

Instantly share code, notes, and snippets.

@technickle
Last active April 9, 2021 18:01
Show Gist options
  • Save technickle/e28a1c3ded5ed7141cc214f01fc55b4d to your computer and use it in GitHub Desktop.
Save technickle/e28a1c3ded5ed7141cc214f01fc55b4d to your computer and use it in GitHub Desktop.
# this shell script extracts all prior versions of a file in a git repository
# make sure to enable execute permissions on this file if your operating system
# requires it: chmod +x extract-all-file-git-commits.sh
# parameters:
# - path-to-file (from root of repository)
# - output directory
# - output file extention
# eg. ./extract-all-file-commits.sh "./public/data/vaccinations/locations.csv" "./output" "csv"
# smarter things it could do:
# auto-detect extension from first parameter
#check to see if output directory exists; if not, make it
[ ! -d "$2" ] && mkdir -p "$2"
#generate the list of commits for the specified filespec and stick it in the temp directory
git log --pretty=format:"%H %cI" --reverse -- $1 > /tmp/extract-all-file-commits.txt
#iterate through the file in the temp folder and output the file from each commit with the timestamp as filename
while IFS=" " read -r item1 item2;
do
git show "$item1:$1" > "$2/$item2.$3";
done < /tmp/extract-all-file-commits.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment