unixorn/git-extra-commands

View on GitHub
bin/git-checkout-by-date

Summary

Maintainability
Test Coverage
#!/usr/bin/env bash
#
# git-checkout-by-date.sh 2012-03-09
#
# From https://stackoverflow.com/a/6990682

set -e

BASEDIR=$PWD
DATESPEC="$1"
shift

DEFAULT_BRANCH=$(git branch -r | awk -F/ '/HEAD/ {print $NF}')

for i in "$@"; do
    cd "$BASEDIR/$i"
    if [ "$DATESPEC" == "$DEFAULT_BRANCH" ]; then
        git checkout master
    else
        git checkout "$(git rev-list -n1 --before="$DATESPEC" "$DEFAULT_BRANCH")"
    fi
done

cd "$BASEDIR"