Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Update Timestamp of files in Checked-out Git Repository
#!/bin/bash -e
# Helper script to update the Last modified timestamp of files in a Git SCM
# Projects working Copy
# When you clone a Git repository, it sets the timestamp of all the files to the
# time when you cloned the repository.
# This becomes a problem when you want the cloned repository, which is part of a
# Web application have a proper cacheing mechanism so that it can re-cache files
# (into a webtree) that have been modified since the last cache.
# @see
# Author: Jeffery Fernandez <>
# Make sure we are not running this on a bare Repository
REPO_TYPE=`git config --list|egrep ^core.bare | awk -F '=' '{ print $2 }'`
if [ "$REPO_TYPE" == "true" ]
echo "Cannot run this script on a bare Repository" && exit 1
echo "Updating Git Repository Last Modified Time-stamp"
# Obtain the Operating System
# Get the last revision hash of a particular file in the git repository
git rev-list HEAD "$1" | head -n 1
# Extract the actual last modified timestamp of the file and Update the time-stamp
# Extract the file revision
FILE_REVISION_HASH=`getFileLastRevision "$1"`
# Get the File last modified time
FILE_MODIFIED_TIME=`git show --pretty=format:%ai --abbrev-commit ${FILE_REVISION_HASH} | head -n 1`
# Extract the last modified timestamp, differently for Linux, FreeBSD and Mac OS X
if [ "$OS" = 'Linux' ]
# for displaying the date in readable format
#FORMATTED_TIMESTAMP=`date --date="${FILE_MODIFIED_TIME}" +'%d-%m-%Y %H:%M:%S %z'`
#echo "Modified: ${FILE_MODIFIED_TIME} | ${FORMATTED_TIMESTAMP} > ${1}"
# Modify the last modified timestamp
touch -d "${FILE_MODIFIED_TIME}" $2
elif [ "$OS" = 'Darwin' ] || [ "$OS" = 'FreeBSD' ]
# Format the date for updating the timestamp
FORMATTED_TIMESTAMP=`date -j -f '%Y-%m-%d %H:%M:%S %z' "${FILE_MODIFIED_TIME}" +'%Y%m%d%H%M.%S'`
#echo "Modified: ${FILE_MODIFIED_TIME} | ${FORMATTED_TIMESTAMP} > ${1}"
# Modify the last modified timestamp
touch -t "${FORMATTED_TIMESTAMP}" $2
echo "Unknown Operating System to perform timestamp update" >&2
exit 1
# Backup and update the "Internal Field Separator" to a newline. This is so that
# we can deal with spaces in file names in the for loop below
# Loop through and fix timestamps on all files in our checked-out repository
for file in $(git ls-files)
updateFileTimeStamp "${file}" "${file}"
# Revert the default delimiter
Copy link

I get "Unknown Operating System to perform timestamp update" error.
How about Windows? Could you add support for it?

Copy link

tst2005 commented Sep 12, 2015

Hello Jeffery,

I needed this kind of script. I decide to improve it with dry-run mode, syncing only if file is not currenly modified, etc.
I only improved the Linux version (because I don't have MacOS X or BSD) to check for now.

Question: Your original code are licensed under what license ?


Copy link

@jeffery GREAT WORK ..... this works great on a local environment, but now I am trying to get this to work inside a TravisCI container and it just does not want to work. Any idea how to modify this for use inside TravisCI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment