Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a documentation .ai file versioned along my project, now I'd like to export every "snapshot" the Git repository holds for that file.

Any batch/semi-automatic solution?

I have tons of commits, have not much intention to export them one-by-one.

like image 738
Geri Borbás Avatar asked Jul 11 '13 15:07

Geri Borbás


Video Answer


2 Answers

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.ai
done

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.

like image 187
Nevik Rehnel Avatar answered Oct 10 '22 15:10

Nevik Rehnel


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

mkdir snapshots
FILE=YourFile.ai
i=1
for 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. :)

like image 41
Kornelius Rohmeyer Avatar answered Oct 10 '22 15:10

Kornelius Rohmeyer