Skip to content

Instantly share code, notes, and snippets.

@cletusw
Created July 10, 2014 18:56
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cletusw/a2381514ecbd0c2c48dc to your computer and use it in GitHub Desktop.
Save cletusw/a2381514ecbd0c2c48dc to your computer and use it in GitHub Desktop.
#!/bin/bash
show_usage () {
echo "Usage: `basename $0` [START [END]]"
echo
echo "Steps through the commit history from START to END,"
echo "then returns to the branch or commit from before execution."
echo
echo "START defaults to the root commit (beginning of history)."
echo "END defaults to current branch/commit."
}
initial_branch=$(git symbolic-ref --short -q HEAD)
initial_commit=$(git rev-parse HEAD)
reset_to=${initial_branch:-${initial_commit:-"master"}}
if [[ ( $1 == "--help" ) || $1 == "-h" ]]; then
show_usage
exit 0
fi
if [ $# -eq 0 ]; then
end=$reset_to
elif [ $# -eq 1 ]; then
start="^$1^"
end=$reset_to
elif [ $# -eq 2 ]; then
start="^$1^"
end=$2
else
show_usage
exit 1
fi
for commit in $(git rev-list $end $start --reverse); do
git checkout $commit
read
done
git checkout $reset_to
@cletusw
Copy link
Author

cletusw commented Jul 10, 2014

Steps through a git repository's history commit by commit (e.g. for a live demo). Inspired by http://stackoverflow.com/questions/3296260/how-to-step-through-a-git-repository#answer-19307936.

@sdondley
Copy link

sdondley commented Oct 15, 2018

Nice script. But it chokes on me after a branch gets merged into master in some cases (not all). To get it back on track:

  1. Manually checkout the repo to the commit after the merge
  2. Run git rev-parse <short_commit_hash_of_current_commit>
  3. ./git-step.sh <long_commit_hash_from_step2>

UPDATE: Disregard this note. I started the git-step.sh script in a detached head state. It was only choking because it got to the commit where my initial detached head was.

@laymanmu
Copy link

omg thank you for this script, so useful.

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