toshiomagic toshiomagic - 6 months ago 44
Git Question

Recover files from previous git commit after git rm

I'm in a really bad situation. I wrote a lot of scripts for a project and accidentally deleted them.

I have 3 branches: master, Dev, App

I develop on the Dev branch, keep a working copy on master, and write application scripts in App. I merged Dev to master, then merged master to App. But when I did I forgot that I had deleted all my app scripts from master and Dev. So I blindly deleted all of my application scripts in App. Now, I want to go back and get those files.

To go back a commit I used

git revert HEAD~1

I then tried

git rm app_*

to untrack the files. But that deleted them all from my folder. And now I can't find them in any commit. How can I get these files back??


Go to the branch you want to fix.

git checkout App

Find the commit before the files were deleted

git log --stat

Remember that SHA and reset to that SHA.

git reset --hard bada55

Repeat for the other branches. You can see what you did and the SHA of what happened using the git reflog command. Helps find points of time that are not on the current branch history.