Search This Blog

Sunday, August 07, 2011

extract historical versions of a file tracked by git

Say, I have a file test.txt being tracked by git.
$git log test.txt
commit 809ebdbd3c44b9310a7a0f67d61897d5d8f9b6d2
Author: rajulocal
Date: Sun Aug 7 14:11:31 2011 -0400

version 15

commit aec45562ad09257ce85eaf00f5a4fa6912e9e51e
Author: rajulocal
Date: Sun Aug 7 14:11:21 2011 -0400

version 14

commit 7fb63da179a2026ba92a8875ff7d3da881ef5854
Author: rajulocal
Date: Sun Aug 7 14:11:11 2011 -0400

version 13

commit 9b3ca5d3d7497e44c012f5ccd5daced2dd90ec12
Author: rajulocal
Date: Sun Aug 7 14:10:51 2011 -0400

version 12

commit d0b5471fa2abce0057cce1d61240e78c09406c04
Author: rajulocal
Date: Sun Aug 7 14:10:40 2011 -0400

version 11

commit e3af4e1a85b4ff07c31c8dcda47eb41c3f3c830b
Author: rajulocal
Date: Sun Aug 7 14:10:26 2011 -0400

version 10

commit be762c0ee45ec6046fa960cace9acae513b80137
Author: rajulocal
Date: Sun Aug 7 14:10:18 2011 -0400

version 9

commit 03ad798434559300cff26f6a238d7b0f05ad53d2
Author: rajulocal
Date: Sun Aug 7 14:09:59 2011 -0400

version 8

commit 94c50cce3b24e572133eeaedc5bfe00aad568aec
Author: rajulocal
Date: Sun Aug 7 14:09:52 2011 -0400

version 7

commit ab63e2bc7b258f702cc8c516c1f16bafc754cf27
Author: rajulocal
Date: Sun Aug 7 14:09:44 2011 -0400

version 6

commit 16c896d19e00267b63537596485074488cd20a85
Author: rajulocal
Date: Sun Aug 7 14:09:37 2011 -0400

version 5

commit 6937331a12209121ba24d111e1ca5128a3c1e836
Author: rajulocal
Date: Sun Aug 7 14:09:21 2011 -0400

version 4

commit 7ec1c7f98a152f6b760ff1561a3b805cc5e374e9
Author: rajulocal
Date: Sun Aug 7 14:09:13 2011 -0400

version 3

commit 5b3788ad1bdef15c1895a6efefcdd1ea96c840e9
Author: rajulocal
Date: Sun Aug 7 14:09:01 2011 -0400

version 2

commit 1306cba057d6cac06ffd39b9234ce72967dc44f8
Author: rajulocal
Date: Sun Aug 7 14:08:35 2011 -0400

version 1


In order to extract the latest 12 versions of test.txt and store each into a different file, I do
$xtract_hist_git 12 test.txt
$ls m??_test.txt
m00_test.txt m02_test.txt m04_test.txt m06_test.txt m08_test.txt m10_test.txt m12_test.txt
m01_test.txt m03_test.txt m05_test.txt m07_test.txt m09_test.txt m11_test.txt

m00_test.txt is the latest copy of test.txt, m01_test.txt is the latest but one copy, m02_test.txt is the latest but two copy etc.,

xtract_hist_git is this little script
$cat ~/bin/xtract_hist_git           
#! /bin/sh
#
# Author : Kamaraju S. Kusumanchi
# Email : kamaraju at gmail dot com
# File : ~/bin/xtract_hist_git
# Date (last modified) : Sun Aug 7 14:28:14 EDT 2011

revs=$1
file=$2

for i in `seq -w 0 $revs`
do
git checkout master~$i $file
mv $file m${i}_$file
done
git checkout master $file

Followers