0

I was trying to remove all untracked files from my Mac machine using the following line

git clean -f -d

After running the above code, all files in my machine got deleted. Is there any ways through which i can revert back all changes?

I have tried "Can I undo a `git clean -fdx`?" but still im unable to recover. if i run git status, it shows all deleted files in red letters though

Community
  • 1
  • 1
  • 1
    **TL;DR: No.** Possible duplicate of [Can I undo a \`git clean -fdx\`?](http://stackoverflow.com/questions/6267180/can-i-undo-a-git-clean-fdx) – cmbuckley Sep 09 '16 at 11:13
  • Pro tip for the future to avoid the issue: for certain dangerous git commands, you can use `--dry-run` param to see what files will get affected, without actually doing the operation. Refer to git documentation for each command. https://git-scm.com/docs/git-clean – jakub.g Sep 09 '16 at 11:25
  • ok thanks. Isn't it possible for me to restore the files back? –  Sep 09 '16 at 11:27
  • git status shows all deleted files in red letters though –  Sep 09 '16 at 11:27
  • What exactly does `git status` say? If their deletions are "not staged for commit", `git reset --hard` restores them. – Martin Nyolt Sep 09 '16 at 12:20
  • it shows files and folders in red colour –  Sep 09 '16 at 12:40

2 Answers2

1

You should have used the git reset --hard and git clean -f commands together. Running both of them makes your working directory match the most recent commit, giving you a clean slate to work with. Although -f is a secure thing that prevents you from accidentally delete everything.

But since it has happened now, there's less scope to recover it via git. Try recovering it using local history of your editor. For instance, I've recovered files from local history while working on Eclipse.

Ankit Bhatnagar
  • 745
  • 5
  • 16
0

To ignore (by overwriting) what is in the working directory and thus recover the files previously committed.

git checkout -f HEAD
Gregg
  • 2,444
  • 1
  • 12
  • 21
  • error: pathspec 'HEAD' did not match any file(s) known to git. –  Sep 09 '16 at 14:10
  • leave off the HEAD. 'git checkout -f' make sure that you don't have 'git checkout -f -- HEAD'. I just tried it and it works on my windows 7 box. – Gregg Sep 09 '16 at 14:12