Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all revisions of a file in git

Tags:

git

unix

I have a git repository, with multiple files in it.

I need to get all the revisions (not diffs) of a specific file foo.bar, and store it as something like 0001-{date}-{sha}-foo.bar, 0002-{date}-{sha}-foo.bar, etc in a new folder.

How can I do that?

like image 951
Dogbert Avatar asked Nov 25 '11 13:11

Dogbert


People also ask

How do I see all changes in git?

Find what file changed in a commit To find out which files changed in a given commit, use the git log --raw command. It's the fastest and simplest way to get insight into which files a commit affects.

Which command is used to view the history of all the changes to a file?

Use --follow option in git log to view a file's history This lists out the commits of both the files. Still, the first commit of the TS file would be a single creation commit, and the last commit of the JS file would be a single deletion one.


2 Answers

Assuming you want the author date, and that a unix timestamp is ok:

i=0
git log --reverse --format=%H\ %at $file |
while read hash date; do
  git show $hash:$file > $(dirname $file)/$i-$hash-$date-$(basename $file)
  : $((i += 1))
done

should do what you want. Change the %at specifier if you want different dates, but unix timestamp is nice since you don't have to deal with spaces in the name. Obviously, there are ways to deal with that, if you choose.

like image 111
William Pursell Avatar answered Nov 15 '22 04:11

William Pursell


You can use git log --pretty="%ct %H" -- foo.bar, which will give you something like:

1322212187 f75e724eec4e11a720d4620b3ca09eff91bb754e
1311666567 d6b578f1b778c67d78322d914d3ab46e02d12f6c
1311584563 cb76c3e39553cf20c9e96ad3d2c963b905d19180
1311321178 bec391005805ca01e9c7b11032b456d426f4fa52
1311321089 b29afbf8c4ca7e8f178cec981b5d3d14f0abeb15
1311081641 74d5747f2094a8f1696344c6c2b94244e52c7cc9

The first column is the commit (unix) timestamp, the second is the commit's SHA. You may change the date format if you want something else ('%ct' in the --pretty argument, RTFM to see options) if you like. From this, it'd be easy to write git commands to check out that specific revision of that specific file and copy it with your naming convention using shellscript - left as an exercise to the reader.

like image 30
Romain Avatar answered Nov 15 '22 05:11

Romain