Git + Rails: How to restore files deleted with "git rm -r"?

ChrisWesAllen picture ChrisWesAllen · Feb 17, 2011 · Viewed 8.8k times · Source

I deleted my db folder in a rails application in with git rm -r

I've tried

git reset HEAD

and

git reset --hard HEAD

but the migration files arent coming back. I tried commiting, then running the reset and still nothing.

What should I do?

Answer

htanata picture htanata · Feb 17, 2011

You can checkout the file from the commit where it still exists. Here's how to do it.

git checkout <commit where the file still exists> -- db
# Example:
git checkout 6936142 -- db

# This also works, but if you have a branch named the same as the file or path,
# it will throw an error.
git checkout 6936142 db