Skip to content

Instantly share code, notes, and snippets.

@i3zhe
Last active December 20, 2015 08:49
Show Gist options
  • Save i3zhe/6103046 to your computer and use it in GitHub Desktop.
Save i3zhe/6103046 to your computer and use it in GitHub Desktop.
Restore a deleted file/path in a Git repo. I met a problem when upgrading a Rails 2 project to 3. I removed the images, javascripts and stylesheets dirs in the public/, and committed the change, but I accidentally forgot to move all the images to asset. So here's a quick way to restore the images I need.
`git rev-list -n 1 HEAD -- <file_path>`
Then checkout the version at the commit before.
`git checkout <deleting_commit>^ -- <file_path>`
Or in one command, if $file is the file in question.
`git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"`
The solution works fine on bash, but zsh has it's own expansion on '^'. So simply replace '^' with '~1', ~X allows you to specify X commits before the specified commit, so ~1 is the commit before, ~2 is two commits before, etc
`git checkout <deleting_commit>~1 -- <file_path>`
another way is to escape '^' with '\^',
`git checkout <deleting_commit>\^ -- <file_path>`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment