Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore a file's modification time in Git

Tags:

git

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.)

like image 513
rampion Avatar asked Mar 16 '10 20:03

rampion


People also ask

Does git preserve file modification time?

Git stores the last modification time for each file, based on its commit history.

Does git restore remove changes?

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.

How do I restore a previous version of a git file?

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.


1 Answers

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.

like image 124
TallOne Avatar answered Sep 19 '22 11:09

TallOne