35

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?

1
  • 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
    Commented Dec 12, 2014 at 20:23

3 Answers 3

61

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.

0
11

This will update the hash of the last commit without changing its message, changes, or timestamp:

git commit --amend --no-edit
4
  • 3
    This would rewrite the history and require a force push.
    – DavidGamba
    Commented Sep 25, 2019 at 17:19
  • @DavidG sure. Of course, you should not try to change the hash of already pushed commits. Commented Sep 25, 2019 at 20:01
  • 5
    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
    Commented Jun 10, 2020 at 10:51
  • 1
    In order to change the timestamp of older commits, this was very useful: stackoverflow.com/questions/454734/…
    – Asensi
    Commented Jun 10, 2020 at 10:53
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.

1
  • 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
    Commented Dec 12, 2014 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.