在Git中恢复文件的修改时间 [英] Restore a file's modification time in Git

查看:151
本文介绍了在Git中恢复文件的修改时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解更新修改时间的默认Git行为每次它改变一个文件,但有时候我想恢复一个文件的原始修改时间。



有没有办法告诉Git (例如,在处理大型项目时,我对 configure.ac 进行了一些更改>,发现autotools不能在我的系统上工作,并且希望将 configure.ac 的原始内容和修改时间恢复到 make 不会尝试用我的破坏的自动工具更新 configure 。)

解决方案

Git不会这样做。就像你的链接FAQ说的那样,它会使用基于时间戳的依赖关系分析工具,比如 make 来破解。



考虑如果旧时间戳被应用到从'旧'提交中签出的文件:




  • make li>
  • 签出一个较旧的分支/标签/提交(这些文件的时间戳会比构建产品更早!)

  • make 现在什么都不做,因为所有的构建产品都比它们的依赖更新



但是,如果你真的想要它,所有的信息都在那里。您可以编写自己的工具来执行此操作。



在您的情况中,只需使用 touch -r configure configure.ac 重置只有 configure.ac 的修改时间,(或者用 touch configure )。




实际上,如果你想练习阅读C代码,这对于读者来说是一个简单的练习。改变时间戳的函数是 utime 或者 utimes 。在git.git克隆中搜索代码以使用这些函数(提示: git grep utime )。如果有一些用途,分析代码路径以找出更新时间戳的时间。


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 does not do this. Like your linked FAQ says, it would break using timestamp-based dependency analysis tools like make.

Think about what would happen if old time stamps were applied to files checked out from ‘old’ commits:

  • make from a clean directory works fine
  • checkout an older branch/tag/commit (the files would have timestamps older than the build products now!)
  • make now does nothing because all the build products are newer than their dependencies

But, if you really want it, all the information is there. You could write your own tool to do it.

In your case, just use something like touch -r configure configure.ac to reset the modification time of only configure.ac, (or bring configure forward in time with touch configure).


Actually this is an easy "exercise for the reader" if you want to practice reading C code. The function that changes timestamps is utime or utimes. Search the code for uses of those functions (hint: git grep utime in a git.git clone). If there are some uses, analyze the code paths to find out when it updates timestamps.

这篇关于在Git中恢复文件的修改时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆