Skip to content

Instantly share code, notes, and snippets.

@thomasjachmann
Created August 26, 2011 08:50
Show Gist options
  • Save thomasjachmann/1173001 to your computer and use it in GitHub Desktop.
Save thomasjachmann/1173001 to your computer and use it in GitHub Desktop.
restore lost git commits
#!/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
@phoet
Copy link

phoet commented Sep 20, 2011

schönes ding, aber es hat mir leider nicht so viel geholfen wie das hier: http://de.gitready.com/advanced/2009/01/17/restoring-lost-commits.html

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