Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jolly-Pirate/be08259ad74bea5fa5012c558aad367d to your computer and use it in GitHub Desktop.
Save Jolly-Pirate/be08259ad74bea5fa5012c558aad367d to your computer and use it in GitHub Desktop.
Retrieve and set the last modification date of all files in a git repository. Solution for https://stackoverflow.com/a/55609950/4063462
#!/bin/sh -e
OS=${OS:-`uname`}
if [ "$OS" = 'Darwin' ]; then
get_touch_time() {
date -r ${unixtime} +'%Y%m%d%H%M.%S'
}
else
# default Linux
get_touch_time() {
date -d @${unixtime} +'%Y%m%d%H%M.%S'
}
fi
# all git files
git ls-tree -r --name-only HEAD > .git_ls-tree_r_name-only_HEAD
# modified git files
git diff --name-only > .git_diff_name-only
# minus https://stackoverflow.com/questions/1370996/minus-operation-on-two-files-using-linux-commands
# only restore files not modified
comm -2 -3 .git_ls-tree_r_name-only_HEAD .git_diff_name-only | while read filename; do
unixtime=$(git log -1 --format="%at" -- "${filename}")
touchtime=$(get_touch_time)
echo ${touchtime} "${filename}"
touch -t ${touchtime} "${filename}"
done
rm .git_ls-tree_r_name-only_HEAD .git_diff_name-only
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment