30

I have an automated build tool that uses the modification date of the file in the output. Is there a way to "git touch" the file and save that to Git without having to actually modify the file?

DavidGamba
  • 3,503
  • 2
  • 30
  • 46
  • 3
    `git` doesn't even track file time stamps, except for commit time stamps, so the answer to this is going to be no. – twalberg Dec 12 '14 at 20:23

3 Answers3

58

I think what you need is touch command if you are on unix operating system.

Git can however allow you to do a empty commit if you use --allow-empty option.

Eg. $ git commit --allow-empty -m "Trigger notification" would cause an empty commit which you can then push and do another deploy.

abhishek77in
  • 1,848
  • 21
  • 41
8

Git works on content hashes so it won't see your change and I doubt it has that functionality. I'd recommend that you echo the current date into that file rather than relying on modified date.

As a sideonote, relying on modification date is going to cause you many more problems as local time can be different between machines.

Srdjan Grubor
  • 2,605
  • 15
  • 17
  • Thanks for the answer, I don't mind the local time issues since the only one that really matters is what the build machine sees, I am also only looking for the day things were modified. – DavidGamba Dec 12 '14 at 20:55
8

This will update hash and timestamp of last commit, without any changing or message dialog.

git commit --amend --no-edit
Pavel Shorokhov
  • 4,485
  • 1
  • 35
  • 44
  • 2
    This would rewrite the history and require a force push. – DavidGamba Sep 25 '19 at 17:19
  • @DavidG sure. Of course, you should not try to change the hash of already pushed commits. – Pavel Shorokhov Sep 25 '19 at 20:01
  • 4
    In order to change the timestamp (and hash) of the last commit, `git commit --amend --no-edit` didn't change the date of the last commit, but `git commit --amend --reset-author --no-edit` did. For more information, in `man git commit`: --reset-author When used with -C/-c/--amend options, or when committing after a conflicting cherry-pick, declare that the authorship of the resulting commit now belongs to the committer. This also renews the author timestamp. – Asensi Jun 10 '20 at 10:51
  • 1
    In order to change the timestamp of older commits, this was very useful: https://stackoverflow.com/questions/454734/how-can-one-change-the-timestamp-of-an-old-commit-in-git/31540373#31540373 – Asensi Jun 10 '20 at 10:53