Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I restore deleted files (undo a `git clean -fdx`)?

Tags:

git

git-clean

People also ask

Can git restore deleted files?

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.

How do I get files back from git clean?

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 I Uncommit a deleted file in git?

In your case you must have used git rm to remove the file, which is equivalent to simply removing it with rm and then staging that change. If you first unstage it with git reset -- <file> you can then recover it with git checkout -- <file> .


No. Those files are gone.

(Just checked on Linux: git clean calls unlink(), and does not backup up anything beforehand.)


git clean -fdxn

Will do a dry run, and show you what files would be deleted if you ran

git clean -fdx

(of course this is only helpful if you know this ahead of time, but for next time)


IntelliJ/Android Studio allows restoring files from local history.


No. "git clean -fdx" will delete all files and directories that git does not track from your working-directory. Because Git does not track these files, it won't have any backups of these files. At least not usually.

If you have done a 'git add' on one of these files relatively recently (but aborted the commit), there is a chance you can find it with 'git fsck --lost-found'. It's worth a try, but don't get your hopes up too much.

In the future you should consider rather committing a few times too often than a few times too seldom. That way you'll at least have a local backup, even if you don't end up pushing these commits to a remote.


If you are using any Jetbrains IDE there's an option to see local history of a file. In case you have done git clean, you can recreate the file and check the local history of the file and restore it from there.

Worked for me for a single file. For a complete directory I don't have any idea how to do that.


As @kusma mentioned above, if you are lucky (if you ever did "git add"), then you can use the following to extract the entire object:

git fsck | awk '{print $3}' | xargs git show | tee searchresults.log

That way, it will look for all the types of chunks, collect the entire diffs and add to a file that you can extract the lost file. In my case, I had lost an entire Java class.