Skip to content

Instantly share code, notes, and snippets.

@manojmj92
Created March 22, 2018 14:53
Show Gist options
  • Save manojmj92/5ff042736f99cdb451404448207a7a31 to your computer and use it in GitHub Desktop.
Save manojmj92/5ff042736f99cdb451404448207a7a31 to your computer and use it in GitHub Desktop.
# add an alias to your bash profile with this code as walk
# alias walk='ruby <your file path.rb>'
# Usage
# walk first
# walk next
# walk prev
case ARGV[0]
when "next"
rev_list = `git rev-list --children --all`
refs = rev_list.scan(/[a-z0-9]{40}(?= )/)
refs.unshift(rev_list[/[a-z0-9]{40}/])
refs.reverse!
head = `git rev-parse HEAD`.chomp
ref_for_next_commit = refs[refs.index(head) + 1]
if ref_for_next_commit
puts `git checkout #{ref_for_next_commit}`
else
puts "You're already on the latest commit."
end
when "prev"
puts `git checkout HEAD^`
when "first"
first_commit = `git rev-list --max-parents=0 HEAD | tail -n 1`
puts `git checkout #{first_commit}`
else
puts "Usage: git-walk next|prev|first"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment