0

I'd like to retrieve all the historical versions of a file in a git repo, and save them into multiple files. I am thinking of making a script to checkout all the commit tags one by one and save the target file, but is there any easier way to do this? Thanks!

xiaolong
  • 3,396
  • 4
  • 31
  • 46

2 Answers2

1
git rev-list --all --objects -- path/to/file.txt

lists you all the blobs associated with the repo path

To get a specific version of a file

git cat-file -p commitid:path/to/file.txt
Andy897
  • 6,915
  • 11
  • 51
  • 86
  • Nit: The `git rev-list` command yields all _commits_ that touch the file in question. The commits can then be resolved to blobs. – Magnus Bäck Jan 05 '15 at 18:38
  • @Andy897, thanks! the output of `git rev-list` includes some lines that include a commit id and a path. When I use `git cat-file` on those commitid, I get an error saying "Path xxxx exists on disk, but not in commitid". Any insights? – xiaolong Jan 05 '15 at 20:05
0
for file in $(git log --format="%H" --all --follow --name-status   -- <path/to/your/file> | egrep '\w' | perl -pe 's/([0-9a-f]{40})\n/\1/' | perl -pe 's/(.{40}).*\s(\S+)$/\1:\2/'); do git cat-file -p $file > content_of_$(echo $file | cut -c -40); done

This assumes that there are no whitespace characters in you file's names or paths. (There should no be anyway.)

Joseph K. Strauss
  • 4,683
  • 1
  • 23
  • 40