git-archive show same commit date in files with different commit date

339 views Asked by At

In git, I have two files with different commit date. But, when I make git-archive, I get the same commit date, why ?

[[ -e git_test ]] && rm -rf git_test
mkdir git_test
cd git_test
git init

# make file01
echo 'f01 $Format:%cd$' > file01.txt
echo 'file01.txt export-subst' >> .gitattributes
git add .gitattributes file01.txt
git commit -m "adding file01"
sleep 1
# make file02
echo 'f02 $Format:%cd$' > file02.txt
echo 'file02.txt export-subst' >> .gitattributes
git add .gitattributes file02.txt
git commit -m "adding file02"

# git archive
git archive HEAD | tar -x -C ..

echo
echo "git log date"
git log --format="%cd" file01.txt
git log --format="%cd" file02.txt

echo
echo "git archive date"
cd ..
cat *.txt

in the output, with git-log the commit date is different (one second), but the files generated with git-archive have the same commit date

git log date
Fri Dec 27 15:17:22 2013 -0300
Fri Dec 27 15:17:23 2013 -0300

git archive date
f01 Fri Dec 27 15:17:23 2013 -0300
f02 Fri Dec 27 15:17:23 2013 -0300
2

There are 2 answers

0
JuanPablo On BEST ANSWER

I solved using archive only over the last modified files

 git archive HEAD $(git diff --name-only HEAD^) | tar -x -C

this way is only modified the commit date (and the file) over the last modified files.

0
Magnus Bäck On

The commit date used in keyword substitution during git archive is that of the commit you're exporting, i.e. the second commit at 15:17:23. Git will not locate the most recent commit that touches a particular file and grab its commit date. That's what happens when you run e.g. git log --format="%cd" file01.txt (well, it'll list the commit dates of all commits touching that file, but in this case there's only one commit).