I am trying to recover a file that was accidentally removed when I merged a branch to the master.
Now, when I try to do a git checkout of a new branch I get this message:
$ git checkout testingNewFormStyle
D module/FormDependencies/src/FormDependencies/Entity/LanguageList.php
Switched to branch 'testingNewFormStyle'
However, I want to restore that file. How can I do this?
If you have deleted the file and already committed the changes, you need to use the ` git checkout` command to restore the file. First, you need to find out the checksum of the commit that deleted the file, and then check out the file from the previous commit.
Even better, in cases like committing a file deletion and then wanting to revert it, Tower's awesome undo feature lets you get the file back by simply pressing CMD-Z!
Git does not apply deleted files when merging an old branch into the master.
You can just use git reset 'commit id contains your deleted file' then merge and push it again. Show activity on this post. You should use git reset HEAD~ and then use git checkout -- <filename> to restore deleted files. You are assuming the deletion was made in previous commit, what if it was deleted long time ago.
You can do, for instance:
git checkout HEAD -- module/FormDependencies/src/FormDependencies/Entity/LanguageList.php
This will checkout the file as it exists in the HEAD refspec (ie, the current branch). If HEAD is not what you want, replace with the appropriate refspec.
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