Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to recover a file in Git [duplicate]

Tags:

git

rm

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?

like image 873
Léo Léopold Hertz 준영 Avatar asked Mar 11 '09 16:03

Léo Léopold Hertz 준영


People also ask

How do I restore a file in git?

Git provides ways to recover a deleted file at any point in this life cycle of changes. If you have not staged the deletion yet, simply run `git restore <filename>` and the file will be restored from the index.

How do I find a lost file in git?

In this case, you can restore the file using either git checkout or git reflog . You can find the hash-ID of the previous commit from the command: git log . In case you don't have the hash ID, you can use the command git reflog .

Does git keep copies of files?

In Git, as with most version control systems, a repository retains a complete copy of the entire project throughout its lifetime. However, unlike most other VCSs, the Git repository provides not only a complete working copy of all the files in the repository but also a copy of the repository itself with which to work.

Can you see deleted files in git history?

Listing all the deleted files in all of git history can be done by combining git log with --diff-filter . The log gives you lots of options to show different bits of information about the commit that happened at that point.


2 Answers

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)!

like image 162
Jakub Narębski Avatar answered Sep 28 '22 00:09

Jakub Narębski


Create a tag or branch at the commit before you deleted fileA, check it out, copy fileA somewhere else, then checkout the newFeature branch again. The rest should be pretty simple.

like image 27
kinghajj Avatar answered Sep 27 '22 23:09

kinghajj