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?
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.
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 .
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.
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.
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)!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With