2

I have a git repo with one file which has new commits added almost daily.

I would like to extract copies of the file as it looked at every commit to a new directory.

Ideally the files would be date-stamped (2017_08_24.txt) according to when the commit was made.

I've already tried git format-patch -o directory --root HEAD but this exports the changelog, not the full file as it looked at the time of the commit.

Thanks

John L. Godlee
  • 559
  • 5
  • 21
  • I’m going to close this as a duplicate since there are enough questions covering how to get a file version from a specific revision. The task to create a script to retrieve and save *all* versions of a file is left to the OP (remember this is not a code-writing service). Combined with `git log` (which can be called to just show commit ids), this shouldn’t be too difficult. – poke Aug 24 '17 at 08:33

3 Answers3

2

git show <commit>:<file> will gove you the contents of a file as it was in a specific commit hash. You could use git log to get all the commits on a file, and then use xargs to pass them on to git show.

The following command line will extract a series of files with the original name and the commit hash concatinated to them:

$ git log --format="%H" /path/to/file | \
xargs -I % sh -c "git show %:/path/to/file > /path/to/target/filename.%"
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

Here's a bash script. It's not fully tested. You can modify it as you like.

#!/bin/bash

branch=$1
file=$2

git log --pretty=%H $branch | while read hash;do 
    object="$branch:$file"
    objecthash=$(git rev-parse $object 2>/dev/null)
    if [ "$object" != "$objecthash" ];then
        git cat-file -p $objecthash > $(git log -1 --pretty=%cd --date=format:%Y%m%d_%H%M%S $hash).txt
    else 
        echo "$file does not exist in $hash"
    fi
done
ElpieKay
  • 27,194
  • 6
  • 32
  • 53
0

For a specific commit you can retrieve the file version like this

git show 23uggf2f8g:myfile.txt > backupDir/oldVersion.txt

But I think that you have to write a script to do that automatically for every commit

Francesco
  • 4,052
  • 2
  • 21
  • 29