简体   繁体   中英

Git: Recovery of files possible after local remove before push?

I wanted to "clean" my git repo before pushing by removing every JPG file, so I entered:

find . | xargs rm *.png

in the git root and now everything is delted . Also my *.py files are deleted, but I do not know why? It is a Linux, Ubuntu machine. Is there any chance to recover my files? Maybe over my OS?

The command you typed is plain wrong:

find .

This command outputs the name of every file and directory below . , including hidden files.

xargs

This command takes its input and runs the command given as its argument, feeding it one line at a time as an argument. Therefore, it will run rm *.png <line1_from_find> , then rm *.png <line2_from_find> , etc.

There is no safeguard like stop on errors, so if you let the command run completely, it unlinked all files and you know have an empty directory tree. And git will not help you, because it works by storing its metadata and current state within a .git directory at the root of the working directory. In which you just deleted all files.

So no, unless you made a copy, either manually or by pushing you state to some other place, it's probably gone, but see below. For future reference, here is the correct command to destroy all files ending in png:

find . -name '*.png' -delete

Optionnaly add -type f before the -delete if you may have directories ending in .png , to filter them out.


Okay, what now: it happens that git marks some of its internal state as read-only, which rm honors if you didn't use rm -f and you might be able to recover some data from that. Go to the .git directory at your working directory's root. It will contain a objects directory, and some files may have survived there.

Those files are raw compressed streams, you can see their content using that command:

 zlib-flate -uncompress <path_to_the_file

(the zlib-flate command comes from qpdf package on ubuntu)

for me the following worked:

$ git checkout -- name_of_deleted_directory

or

$ git checkout -- path_to_local_delected_directory

In my case, I deleted a directory by mistake and I didn't change any code in that directory but I had changed code in other directories of my git repo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM