Export every revision of a single file from a Git repository? Export every revision of a single file from a Git repository? shell shell

Export every revision of a single file from a Git repository?


To export all revisions of a file to a given folder, you can use this loop in Bash:

for sha in `git rev-list HEAD -- path/to/file`; do    git show ${sha}:path/to/file > path/to/exportfolder/${sha}_doc.aidone

You can customize this, of course.

Copy the snippet into a text file, and replace path/to/file with the actual path to the file you want to export (inside the repo). Replace path/to/exportfolder with the actual path to where you want to export the files to (the export folder must exist). You can also modify the exported filename, in this version it uses the format "_doc.ai"; just make sure to use quotes if you want to use a file or path name with spaces.

This version will start at the currently checked out revision (HEAD) and walk over the history to all revisions reachable from HEAD in the ancestry tree. That means you should check out the most recent revision you want to start exporting from.


Quick, but since no one else came up with something:

mkdir snapshotsFILE=YourFile.aii=1for COMMIT in $(git log --oneline $FILE | cut -f 1 -d " "); do   git checkout $COMMIT $FILE;   cp $FILE snapshots/$i-$COMMIT.ai;   (( i = i + 1 ))done

Then your directory snapshots contains all versions of the file. The names consist of a number 1..n which increases with the age of each version and the respective commit together with the file extension .ai.

P.S.: Don't forget to update FILE after that (git checkout HEAD $FILE) and don't add the folder snapshots to your repository. :)