1

I have some files in my Git repository:

...
-rw-r--r--  1 dimti dimti 11489 мар  2 21:54 wp-settings.php
...

After executing the command

git archive --format=tar HEAD > repo.tar

I fetch my files in the tar archive, but the modify time is wrong:

-rw-r--r--  1 dimti dimti 11115 мар  5 21:55 wp-settings.php

How can I say to Git archive - preserve the modification time on files in the tar archive?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Candidates for the canonical question: *[What's the equivalent of Subversion's "use-commit-times" for Git?](https://stackoverflow.com/questions/1964470/)* (2009) and *[Checking out old files WITH original create/modified timestamps](https://stackoverflow.com/questions/2179722)* (2010). Mercurial has [the Timestamp extension](https://stackoverflow.com/a/7809151) (though that does not help much). – Peter Mortensen Sep 17 '21 at 10:10

1 Answers1

3

You can try and tag it first, in order to use that tag, or use the commit ID

The git archive man page mentions:

git archive behaves differently when given a tree ID versus when given a commit ID or tag ID.

  • In the first case (tree ID like HEAD) the current time is used as the modification time of each file in the archive.
  • In the latter case (commit ID or tag ID) the commit time as recorded in the referenced commit object is used instead.

So in your case (using "How to retrieve the hash for the current commit in Git?"):

git archive --format=tar $(git rev-parse HEAD) > repo.tar
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Hi. Your answer - not solve my question. Yes. i added "HEAD" into the command git archive. But my question - how to preserve modify times on files after execution git archive (files in tar archive - with other modify time vs original files in git repo) – Alexander Demidov Mar 06 '15 at 10:15
  • @AlexanderDemidov the tar would only keep the original time if you are *not* using `HEAD` for the `git archive` – VonC Mar 06 '15 at 10:19
  • 2
    NB: using a commit ID like this does *not* use the exact time a specific file was modified but the date of the referenced commit. If one needs to restore all the original dates then something like this is needed (currently): http://stackoverflow.com/a/22638823/1905491 – stefanct Mar 26 '16 at 21:14