Search code examples
gitrm

Unable to recover a file in Git


Possible Duplicate:
Restore a deleted file in a Git repo

I have two branches in my Git, master and newFeature. At the branch newFeature, I removed the fileA physically first in terminal and then in Git by

git rm fileA

Subsequently, I run

git add .
git commit

Right now, I need the fileA again. I had the idea that I can get it back, by simply switching to the branch master. I was apparently wrong, since I cannot find the fileA.

How can I get the fileA back with Git?


Solution

  • First, you need to find where you have last version of fileA. You can use "git log -p" or "git whatchanged" to check when it was deleted, or you can use "git ls-files <revision> -- fileA" to check if file is present in given commit, where '<revision>' can be master or newFeature^ (newFeature^ means parent of newFeature).

    Then you need to check it out, either using

    $ git checkout <revision> -- fileA
    

    or redirect "git show" output

    $ git show <revision>:fileA > fileA
    

    Don't forget to add file to git (if needed)!