3

Can I commit file modification changes with Git? For example, I touched the file test.txt (touch test.txt) which changes the timestamp without changing its content. Now, can I commit this change in file modification time with Git?

I need to synchronize the timestamp of files between my local and remote repository.

The topic Restore a file's modification time in Git doesn't solve this problem. Actually they are complete opposite. While that topic talks about restoring timestamps after a successful commit, this topic wants to commit only the timestamp (not any change in content, which is not Git's default behavior), i.e a git commit command should detect a change in the timestamp and perform commit even if there is no change in the content.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jahid
  • 21,542
  • 10
  • 90
  • 108
  • 1
    Possible duplicate of [Restore file's modification time in git](http://stackoverflow.com/questions/2458042/restore-files-modification-time-in-git) – Sven Marnach Feb 03 '16 at 16:49
  • 1
    @SvenMarnach : That's totally a different topic. – Jahid Feb 03 '16 at 16:52
  • What's the point? `git checkout` will set the file modification time of the file in the working tree to the current time if it needs to update the file's content. And this happens everytime you change branches. If you really need to keep the modification time of a special file then put it into another file and commit the new file as usual. – axiac Feb 03 '16 at 17:08
  • As I'm sure you know, git works with diffs, not timestamps. Timestamps really aren't relevant in git's view of the world, AFAIK. So I hate to ask "Why?", but, um, why? What are you trying to achieve? (I.e. this sounds like an X/Y problem.) – Erick G. Hagstrom Feb 03 '16 at 17:48
  • 1
    @ErickG.Hagstrom : I use git to sync my site on localhost and remote. And the site uses timestamp for certain things. And thus several things are different in local and remote, as I use the `touch` command to update timestamp of several files deliberately without changing their content at all. – Jahid Feb 03 '16 at 17:55
  • @axiac : `git checkout will set the file modification time of the file in the working tree to the current time` that's not a problem, If I can somehow make git detect the modification time change as a change and perform commit, that will be enough. – Jahid Feb 03 '16 at 18:04
  • I think the issue I linked is strongly related (I'd even say is essentially the same). If you would have read it, you would have learned that git doesn't track timestamps at all, so it can't detect whether the timestamp has changed. – Sven Marnach Feb 03 '16 at 19:47
  • @SvenMarnach even if the answer is the same, does it make the question a duplicate? – 1615903 Feb 04 '16 at 05:57
  • @SvenMarnach : `would have learned that git doesn't track timestamps at all, so it can't detect whether the timestamp has changed` That doesn't make this question a duplicate. `It can not be done` is not actually a proper answer, there's workaround for almost everything. Therefore, the intention and context of that topic and this one is entirely different. – Jahid Feb 04 '16 at 06:15
  • 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:11

1 Answers1

2

Ok, this is totally blue sky, but if you

  • Created a file to hold timestamps
  • Created a clean filter to build that file and commit it
  • Created a smudge filter to read that file and update timestamps on checkout

I think you could make this happen. Obviously it's totally roll-your-own, but it seems like a possibility.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Erick G. Hagstrom
  • 4,873
  • 1
  • 24
  • 38