I understand the default Git behaviour of updating the modification time every time it changes a file, but there are times when I want to restore a file's original modification time.
Is there a way I can tell Git to do this?
(As an example, when working on a large project, I made some changes to configure.ac
, found out that autotools doesn't work on my system, and wanted to restore configure.ac
's to its original contents and modification time so that make
doesn't try to update configure
with my broken autotools.)
Git stores the last modification time for each file, based on its commit history.
By default, the git restore command will discard any local, uncommitted changes in the corresponding files and thereby restore their last committed state. With the --staged option, however, the file will only be removed from the Staging Area - but its actual modifications will remain untouched.
There are two ways to use the git checkout command. A common use is to restore a file from a previous commit, and you can also rewind your entire tape reel and go in an entirely different direction.
Restore the modificaton time of a list of files to the author date of the their last commit with
gitmtim(){ local f;for f;do touch -d @0`git log --pretty=%at -n1 -- "$f"` "$f"; done;}; gitmtim configure.ac
It will not change directories recursively, though.
If you want to change a whole working tree, e.g. after a fresh clone or checkout, you may try
git log --pretty=%at --name-status --reverse | perl -ane '($x,$f)=@F;next if !$x;$t=$x,next if !defined($f)||$s{$f};$s{$f}=utime($t,$t,$f),next if $x=~/[AM]/;'
NB: I grepped for utime in builtin/clone.c and got no matches.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With