7

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.

Geri Borbás
  • 15,810
  • 18
  • 109
  • 172

2 Answers2

12

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.

Nevik Rehnel
  • 49,633
  • 6
  • 60
  • 50
7

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. :)

  • Thanks, will definietly try these. – Geri Borbás Jul 12 '13 at 14:21
  • They seems - http://prntscr.com/1f08t1 - not to be in chronological order. How can I ensure the right order? Can you edit the answer? – Geri Borbás Jul 12 '13 at 15:30
  • They are created chronological, but most likely faster than time stamp resolution of your file system (which is for example 1 second for ext2/ext3 - don't know about MAC OS) or the file browser only sorts the files based on seconds not milliseconds. I'll edit the answer and add a prefix, which shows the order of the files. – Kornelius Rohmeyer Jul 12 '13 at 16:41
  • I also added a HEAD checkout after the loop to have the original file unchanged. – Geri Borbás Jul 12 '13 at 17:01
  • I don't intended to steal you time, but can you help me implement the second feature? https://gist.github.com/eppz/5986026 To have this feature but with the whole project. Some local clone with --no-checkout or something... – Geri Borbás Jul 12 '13 at 17:05
  • Thank you for this! I ended up using this but I changed `i=1` to `i=1000` to ensure that the files are sortable alphabetically. – psmith Mar 25 '22 at 01:25