Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to undelete a file previously deleted in git's history?

Using Chris's answer on another question I could prepend a snapshot-history to my git repository. Since one of the files is not part of my history but only in the snapshots, the first original commit now also contains the deletion of this file. How can I undo that?

At first I thought this was the opposite of How do I remove sensitive files from git’s history, but actually I don't want to insert a file into history but just remove the deletion from history.

like image 355
Tobias Kienzler Avatar asked Jun 30 '10 14:06

Tobias Kienzler


People also ask

How do I restore a previously deleted file?

To Restore That Important Missing File or Folder:Type Restore files in the search box on the taskbar, and then select Restore your files with File History. Look for the file you need, then use the arrows to see all its versions. When you find the version you want, select Restore to save it in its original location.

How do I undo a git delete?

You can restore a deleted file from a Git repository using the git checkout command. If you do not know when a file was last deleted, you can use git rev-list to find the checksum of the commit in which that file was deleted. Then, you can check out that commit.

Can old deleted files be recovered?

If you accidentally and permanently delete a file, you can recover it using reliable data recovery software and retrieve the file straight from the hard drive. Although this only retrieves the most recent version of the deleted file.

Can permanently deleted history be recovered?

The easiest method is to do a system restore. If the internet history was deleted recently system restore will recover it. To get system restore up and running you can go to the 'start' menu and do a search for system restore which will take you to the feature.


2 Answers

git checkout <commit> <filename> 
like image 119
kubi Avatar answered Oct 11 '22 13:10

kubi


I got it:

git tag originalHead # just in case git rebase -i <id of the parent of the commit that deleted the file> # change pick to edit for that commit git checkout <id of the previous commit> <filename> # thanks for reminding, kubi git commit --amend git rebase --continue git tag -d originalHead 

edit unfortunately this will leave all tags at the old timeline, see here

like image 36
Tobias Kienzler Avatar answered Oct 11 '22 13:10

Tobias Kienzler