Skip to content

Instantly share code, notes, and snippets.

@jonbrennecke
Created September 4, 2014 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonbrennecke/2a0e33d12cb092770971 to your computer and use it in GitHub Desktop.
Save jonbrennecke/2a0e33d12cb092770971 to your computer and use it in GitHub Desktop.
Recover file contents from "git reset --hard" (file names are another story)
var cp = require('child_process'),
fs = require('fs'),
execSync = require('exec-sync');
var fsck = cp.spawn("git", ["fsck", "--cache", "--unreachable","$(git for-each-ref --format=\"%(objectname)\")"])
var stdout = '';
fsck.stdout.on('data',function(data){
stdout += data.toString();
});
fsck.on('close',function(){
var commits = stdout.match(/(.*)\n/g).map(function(line){
var match = line.match(/blob\s+(.*)\n/)
return match ? match[1] : ""
});
console.log(commits.length)
commits = commits.slice(600,commits.length); // test
commits.forEach(function(commit){
if (commit) {
var output = execSync('git show ' + commit);
fs.writeFile( __dirname + "/dumps/" + commit, output, function(err){
if (err)
throw err
});
}
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment