Restore file timestamps from Git

April 21st 2023 GitHub Git

Git doesn't restore file timestamps by default. So the file timestamp indicates when the file was cloned locally. And in most cases that's fine. But such a timestamp will prevent you from detecting if a file on a remote server has changed since it was last committed.

Fortunately, there's an easy way to restore the file timestamps to the time of the last commit. You can use the git-restore-mtime script. If you need to run the script in a GitHub Actions workflow, there's even a custom action available that makes it easier to use.

Still, you have to know that the script requires full repository history to work and by default the checkout action in GitHub Actions doesn't fetch it. To change that, you need to set the fetch-depth to 0:

- name: Checkout
  uses: actions/checkout@v3
    fetch-depth: 0

As the very next step, you should run the git-restore-mtime action, before doing any actions that depend on the correct file timestamps:

- name: restore timestamps
  uses: chetan/git-restore-mtime-action@v2

If you need to restore the file timestamps in your GitHub Actions workflows (e.g., for backups of remotely edited files to work correctly), you can use the git-restore-mtime action. Just make sure to adjust the fetch depth of the checkout action before that.

Get notified when a new blog post is published (usually every Friday):

If you're looking for online one-on-one mentorship on a related topic, you can find me on Codementor.
If you need a team of experienced software engineers to help you with a project, contact us at Razum.
Creative Commons License