Last active
December 20, 2015 08:49
-
-
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.
This file contains hidden or 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
`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