Skip to content

Instantly share code, notes, and snippets.

@trevordixon
Created August 2, 2013 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trevordixon/6143665 to your computer and use it in GitHub Desktop.
Save trevordixon/6143665 to your computer and use it in GitHub Desktop.
Resets multiple git repositories to the commit each would have been at at a given time.
#!/bin/bash
# Usage: git-reset-to-date date repo1 repo2 ...
# Example: git-reset-to-date "7/14/2013 14:00" repo1 repo2 repo3
require_clean_work_tree () {
# Update the index
git update-index -q --ignore-submodules --refresh
err=0
# Disallow unstaged changes in the working tree
if ! git diff-files --quiet --ignore-submodules --
then
echo >&2 "cannot $1: you have unstaged changes."
git diff-files --name-status -r --ignore-submodules -- >&2
err=1
fi
# Disallow uncommitted changes in the index
if ! git diff-index --cached --quiet HEAD --ignore-submodules --
then
echo >&2 "cannot $1: your index contains uncommitted changes."
git diff-index --cached --name-status -r --ignore-submodules HEAD -- >&2
err=1
fi
if [ $err = 1 ]
then
echo >&2 "Please commit or stash them."
exit 1
fi
}
date=$1
shift
for repo in "$@"
do
pushd $repo > /dev/null
require_clean_work_tree "reset $repo"
popd > /dev/null
done
for repo in "$@"
do
pushd $repo > /dev/null
commit_info=$(git log --all -1 --before="$date")
commit=$(echo "$commit_info" | head -1 | cut -d " " -f 2)
echo -n "$repo: "
git reset --hard $commit
echo -e "$commit_info\n"
popd > /dev/null
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment