Skip to content

Instantly share code, notes, and snippets.

@jemus42
Created October 26, 2023 12:21
Show Gist options
  • Save jemus42/0150025816fae8eb29e842148f603411 to your computer and use it in GitHub Desktop.
Save jemus42/0150025816fae8eb29e842148f603411 to your computer and use it in GitHub Desktop.
git timetravel to a specific date
git-timetravel () {
# If first argument is empty you dun goof'd
if [ -z "${1}" ]
then
echo "Must provide a valid timestamp in roughly ISO 8601"
echo "Example: git-timetravel "2022-10-01 12:00" main
exit 1
fi
# Guess branch if unset, use currently checked out branch
if [ -z "${2}" ]
then
BRANCH=$(git rev-parse --abbrev-ref HEAD)
echo "Guessing branch: ${BRANCH}"
else
BRANCH=${2}
fi
# Via https://stackoverflow.com/questions/6990484/how-to-checkout-in-git-by-date
# git checkout 'master@{2022-10-01 18:30:00}' is shorter but can go back 90 days max apparently.
git checkout $(git rev-list -n 1 --first-parent --before="${1}" ${BRANCH})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment