Skip to content

Instantly share code, notes, and snippets.

@technickle
Last active April 9, 2021 18:01
Show Gist options
  • Select an option

  • Save technickle/e28a1c3ded5ed7141cc214f01fc55b4d to your computer and use it in GitHub Desktop.

Select an option

Save technickle/e28a1c3ded5ed7141cc214f01fc55b4d to your computer and use it in GitHub Desktop.

Revisions

  1. technickle revised this gist Apr 9, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions extract-all-file-git-commits.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    # 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)
  2. technickle renamed this gist Apr 9, 2021. 1 changed file with 0 additions and 0 deletions.
  3. technickle created this gist Apr 9, 2021.
    22 changes: 22 additions & 0 deletions extract-all-file-commits.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    # this shell script extracts all prior versions of a file in a git repository

    # 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