Skip to content

Instantly share code, notes, and snippets.

@RonaldFindling
Created January 1, 2016 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RonaldFindling/80055965f3c9c8d0f43c to your computer and use it in GitHub Desktop.
Save RonaldFindling/80055965f3c9c8d0f43c to your computer and use it in GitHub Desktop.
Recover staged and uncommited files after deletion by git reset --hard
#get hash-values for all unreachable files sorted by timestamps by Mark Longair http://stackoverflow.com/questions/7374069/undo-git-reset-hard-with-uncommitted-files-in-the-staging-area
find .git/objects/ -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort | cut -d " " -f 3 | sed "s/\.git\/objects\/\(.*\)\/\(.*\)/\\1\\2/" > file_hashes_to_recover.txt
#create git restore commands
# awk '{FS="\t"} {print "git show "$1" > recovered"FNR".pdf"}' file_hashes_to_recover.txt > file_recovery_commands.txt
awk '{FS="\t"} {print "git show "$1" > recovered"FNR}' file_hashes_to_recover.txt > file_recovery_commands.txt
#run the restoration commands
bash file_recovery_commands.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment