0

I'm trying to figure out how to set the files' timestamps in my repository. On a Git push I want my files' last modified dates to be in the past rather than the time of upload.

I can manually set the commit date easily enough, but that just means I have a days old commit on my remote repository with files that have been modified just now.

I know there are issues with this from searching about similar topics, but this is a personal repository and I would just like to figure this one out. Where does Git draw the file modified data from on a push? Is there any script or extension that will let me do what I want to?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
StolenSheep
  • 57
  • 3
  • 13
  • Files don't have timestamps, in git. Only commits have timestamps. That's why all those other questions wind up either giving up, or adding an auxiliary file containing "timestamps you may set into your files after you receive them" (the aux file may be *just* the timestamps, or a program that does the setting as well as the stamp values). Incidentally, the date on a file that git has to modify during a `git checkout` is just the current date on your computer, i.e., git just writes the file and lets your computer stamp it as "now". – torek Mar 29 '16 at 16:30
  • Here's a script that might do what you're looking for: https://gist.github.com/jeffery/1115504 – paulsm4 Mar 29 '16 at 16:32
  • On a git push I want my files' last modified dates to be in the past rather than the time of upload. Git does not care about the time-stamp of your files.. – CodeWizard Mar 29 '16 at 16:39
  • Git may not care but clearly somewhere along the line when you push to GitHub something affixes the file modified date to the moment of the push. Whether it's git functionality or something GitHub puts there is part of what my question is about. I'm just looking for a way to set the file modified data to be something other than the current time. – StolenSheep Mar 29 '16 at 17:32
  • 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

1

From Why isn't Git preserving modification time on files?:

Modification time on files is a feature that affects build tools. Most build tools compare the timestamp of the source(s) with the timestamp of the derived file(s).

If the source is newer, then a rebuild takes place, otherwise nothing happens. This speeds up the build process a lot.

Now consider what would happen if you check out another branch, and modification times were preserved. We assume you already have a fully-built project.

If a source file on that other branch has a timestamp that is older than that of the corresponding derived file, the derived file will not be built even if it is different, because the build system only compares modification times.

At best, you'll get some kind of weird secondary error; but most likely everything will look fine at first, but you will not get the same result as you would have with a clean build. That situation is unhealthy since you really do not know what code you are executing and the source of the problem is hard to find.

You will end up always having to make a clean build when switching branches to make sure you are using the correct source.

(Git bisect is another Git procedure that checks out old and new revisions where you need a reliable rebuild.) Git sets the current time as the timestamp on every file it modifies, but only those. The other files are left untouched, which means build tools will be able to depend on modification time and rebuild properly. If build rules change, that can cause a failure anyway, but that is a far less common problem than accidentally not rebuilding.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CodeWizard
  • 128,036
  • 21
  • 144
  • 167