Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git restore a deleted file after merge

Tags:

git

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?

like image 974
andreea115 Avatar asked Feb 11 '14 10:02

andreea115


People also ask

Can you restore deleted file in git?

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.

How do you undo a delete in git?

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!

Does git merge delete files?

Git does not apply deleted files when merging an old branch into the master.

How do I recover files from a git push?

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.


1 Answers

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.

like image 166
fge Avatar answered Oct 16 '22 18:10

fge