Skip to content

Instantly share code, notes, and snippets.

@sloanlance
Created May 22, 2023 15:08
Show Gist options
  • Save sloanlance/6ae879209ff488a26f786b240e06d3de to your computer and use it in GitHub Desktop.
Save sloanlance/6ae879209ff488a26f786b240e06d3de to your computer and use it in GitHub Desktop.
Set timestamps of files in a cloned git repository to when they were last committed.
#!/bin/sh --
# Set timestamps of files in a cloned git repository to when they were last committed.
# Default: All git-controlled files in current directory, recursively
if [ ${#} = 0 ]; then
files="$(git ls-files -z | xargs -0 echo)"
else
files="${@}"
fi
for file in ${files}; do
time="$(git log --pretty=format:%cd -n 1 \
--date=format:%Y%m%d%H%M.%S --date-order -- "${file}")"
if [ -z "${time}" ]; then
echo "ERROR: skipping '${file}' — no git log found" >&2
continue
fi
echo "${file} → ${time}"
touch -m -t "${time}" "${file}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment