Created
October 26, 2023 12:21
-
-
Save jemus42/0150025816fae8eb29e842148f603411 to your computer and use it in GitHub Desktop.
git timetravel to a specific date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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