-
-
Save thenoseman/1173309 to your computer and use it in GitHub Desktop.
restore lost git commits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Helps recovering lost commits, eg when you've: | |
# - dropped a stash | |
# - reset a branch to an earlier commit, dropping the changes with --hard | |
# | |
# Shows which files have been changed in which dangling commit and prints | |
# the commands to view details of and restore a commit. | |
# | |
# When given a parameter, only lists commits that changed a file matching | |
# the parameter. | |
# | |
# see: | |
# - http://stackoverflow.com/questions/89332/recover-dropped-stash-in-git | |
# - http://freeasinbeard.org/post/318519782/reflog-and-fsck-your-git-disaster-recovery-toolkit | |
# - http://progit.org/book/ch9-7.html | |
changed_file = ARGV[0] | |
puts "searching for changes in file #{changed_file}" if changed_file | |
dangling_commits = `git fsck | awk '/dangling commit/ {print $3}'`.strip.split | |
dangling_commits.each do |rev| | |
show = `git stash show #{rev} 2>/dev/null` | |
if changed_file.nil? || show =~ /#{changed_file}/ | |
puts | |
puts "CHANGES in dangling commit #{rev}:" | |
puts show | |
puts "view details with: `git stash show -v #{rev}`" | |
puts "restore with: `git stash apply #{rev}`" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment