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

1
  • 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. Commented Aug 24, 2017 at 8:33

3 Answers 3

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.%" 
Sign up to request clarification or add additional context in comments.

Comments

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 

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.