Skip to content

Instantly share code, notes, and snippets.

@shaiguitar
Last active December 30, 2015 18:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaiguitar/7867796 to your computer and use it in GitHub Desktop.
Save shaiguitar/7867796 to your computer and use it in GitHub Desktop.
Using git bisect to find the original commit that a piece of code/text entered the repo. Need this because many times you want to trace back when it was added, but when running a git show/blame you end up finding commits that just move that chuck around in the file/to different files etc.
# USAGE:
# reason for this is because sometimes you want to know when the originating code/comment/whatever was brought into the repo
# but you end up following a trace of lots of commits that just moved around the original chunk of code.
# this uses git bisect to trace back to the original commit that has this text in the repo.
# depends on ack, but you could easily change this to use `grep -r`
#
# $ git-find-original "some search string that you want to find the original commit that had this exact text"
match_arg="$@"
continue_bisect() {
previous_commit=$1
current_commit=$(git rev-list HEAD |head -1)
if [ "$previous_commit" = "$current_commit" ]; then
# we've reached the commit we want
echo
echo "============================================="
echo "run a git show or what have you. you're here."
echo "============================================="
echo
return
fi
ack "$match_arg"
ret=$?
# successful ack, we want to mark as bad, continue until we do
# not find any match, and then mark it good
if [ $ret -eq 0 ]; then
git bisect bad
continue_bisect $current_commit
else
git bisect good
continue_bisect $current_commit
fi
}
initial_commit=$(git rev-list HEAD | tail -n 1)
git bisect start
git bisect good $initial_commit
continue_bisect
@shaiguitar
Copy link
Author

Actually looks like this is the same thing:

git log -S "string to find" --oneline | tail -n 1

Thanks to https://twitter.com/michaelfairley

@shaiguitar
Copy link
Author

Looks like the git bisect approach might be faster than git log -S though. Could use more benchmarks, but does anyone really care? Was for me, but it does depend on git commit history length for the repo.

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