Skip to content

Instantly share code, notes, and snippets.

@hanvari
Created January 20, 2022 21:44
Show Gist options
  • Save hanvari/60ae01468e48a8b5c77f9dab9fb37545 to your computer and use it in GitHub Desktop.
Save hanvari/60ae01468e48a8b5c77f9dab9fb37545 to your computer and use it in GitHub Desktop.
Checkout the latest commit before a given date/deadline

Checkout Latest Commit Before a given Date/Deadline

If you need to checkout the latest commit before a given date or deadline in a git repository:

First, save the desired deadline (yymmddhhmm) an environment variable:

export DEADLINE="2111081100" #yymmddhhmm

Second, run this command to checkout the lates commit before the defined deadline:

git checkout "$(git log --date=format-local:'%y%m%d%H%M' --format="%ad %h %d %s" | \
awk -v deadline="$DEADLINE" '$1<deadline' | head -n1 | awk '{ print $2}')"

References Consulted:

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