Restore files deleted with git clean -df

后端 未结 3 591
北海茫月
北海茫月 2021-01-25 10:39

I was trying to clear my git after adding files from the wrong directory and ran the git clean -df function. This resulted in all my user data being deleted.

I haven\'t

3条回答
  •  鱼传尺愫
    2021-01-25 10:59

    If you're using Eclipse, and you're only missing code or text, don't despair, not all may be lost. Go to:

    [WORKSPACE]/.metadata/.plugins/org.eclipse.core.resources/.history
    

    Then use grep (*nix), FINDSTR (Windows) or a similiar tool to search for text snippets you know to have occurred in the deleted files. Eclipse keeps a lot of local history revisions around, so if you worked on these files recently, you will probably have a lot of matches. Then you need to find most recent one of these, which can be a bit tricky.

    To give an example: After doing a quick git clean -d -fx to show this dangerous feature to a colleague, I was missing an untracked java code file, of which I knew it contained the String XrefCollector. I was able to find the last local history revision by doing this:

    find . -printf "%T@ %p\n" | sort -n | cut -d" " -f2 | xargs grep -l XrefCollector | tail -n1
    

    Here, find prepends the date (epoch) to the files, sort sorts (numerically), cut removes the date again, and xargs passes the filenames on to grep, which searches for the desired string. Finally, tail only shows the last line of output.

提交回复
热议问题