5

I'm a beginner in GIT and didn't know how powerful this program is... I erased 8 Gb from several commands.
As I remember I typed the following lines:

$git config --global user.name "my name"
$git config --global user.email "my email"
$git add *.c
$git commit -m
$git status

then I saw a lot of files tracked and untracked, and I tried to remove them (from git). I didn't know I will remove them from my PC. So I did the following:

$git rm
$git rm --cached *.c

this way I removed all the tracked files. So I continued:

$git clean -f
$git clean -f -d
$git clean -f -x
$git clean -d -x -n

this way I removed some of the untracked files, the ones that weren't used by system (I'm using windows). And now this is what I have:

$git status
On branch master
Initial commit
Untracked files:
 (use "git add <file>..." to include in what will be commited)
 .gitconfig
 AppData/
 Desktop/
 Favourites/
 NTUSER.DAT
 NTUSER.DAT{some characters and numbers}
 ntuser.dat.LOG1
 ntuser.dat.LOG

$git checkout
fatal: You are on a branch yet to be born

Maybe I used more commands but I don't remember! I just want to recover my old files, and I will stop using this lovely Git software.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
user1769195
  • 195
  • 1
  • 5
  • 3
    You'll never get the untracked files back from git, since they aren't tracked they aren't recorded. – tpg2114 Oct 23 '12 at 18:36
  • 19
    Blindly typing commands will always end up here – sciritai Oct 23 '12 at 18:40
  • 3
    I hope you have some recent backup of your computer... – Messa Oct 23 '12 at 18:42
  • 10
    If it's really, really important to you that you get your files back, turn the computer off immediately, boot from a linux livecd, and use an undeletion utility to try to recover things. Your mileage will vary and chances are data has already been lost. If it's not that important, just recreate and move on. @Messa: course not, nobody ever does. – Wug Oct 23 '12 at 18:42
  • a most destructive beginner, but you'll be fine *so long* this isn't your repository – prusswan Oct 23 '12 at 18:43
  • btw, `git init`(?) in `%HOME%` is not a very good idea – prusswan Oct 23 '12 at 18:46
  • 9
    Please guys, hes asking for help at a time which obviously isn't very fun.. no need to keep telling what a bad idea that was. – Steoates Oct 23 '12 at 18:52
  • 2
    c'mon everyone needs a little schadenfreude in their day now and again. `cackles manically` – kayaker243 Oct 23 '12 at 21:01
  • Thanks! Fortunately my system is fine for now, after a reboot, even I didn't recover my files(of course I don't have a backup), just saved the important files. Enough GIT for me for now! Have a nice day! – user1769195 Oct 23 '12 at 21:23
  • @user1769195 You should run a system restore. That way you can get your old files back which you didn't save. Step 1 : Backup all your files. Step 2: Run system restore. Happy recovery! Link:http://windows.microsoft.com/en-US/windows7/products/features/system-restore –  Feb 04 '13 at 23:31
  • I come from SVN, and I just want to point out that Git is strongly safer than SVN. Don't say Git is guilty because you missused it; if you `svn delete`, you get to the same troubles. Give it a chance, but use it wisely :-). – Alain Tiemblo Aug 12 '14 at 21:17
  • @AlainTiemblo: Actually `svn delete` protects against this: "The command will not remove any unversioned or modified items; use the `--force` option to override this behavior." (Of note, it seems like OP was using `--force` options also...) – Ben Voigt Oct 02 '14 at 02:15
  • 1
    I disagree to: "*This question is unlikely to help any future visitors*" this disaster will prevent several others, including me! – Cutton Eye Jan 23 '20 at 16:55

1 Answers1

86

Oh boy. I see what you did, and it's not pretty. Let's read the documentation for "git clean":

git-clean - Remove untracked files from the working tree

This means that git-clean deletes files that it cannot restore. Git has some safety measures in place so that you don't accidentally run git clean -- it won't delete directories unless you specify -d, won't delete ignored files unless you pass -x, and won't do anything at all unless you specify -f.

It looks like you turned your home directory into a Git repository, committed the *.c files, and then deleted everything else.

It's basically like running rm -rf *, or del /s *.* in Windows. Don't do that.

Solution

Restore from backup.

If you don't have a backup, then this is a painful object lesson in why we have backups, and you will have to try to recover the deleted files -- and you must turn off your computer and not boot into Windows until this is complete.

Note on "untracked files"

An "untracked file" is a file that is not part of your Git repository. I can see how if you think that untracked files are part of your Git repo, you will try increasingly dangerous things until they are deleted. The untracked files were never part of your Git repo to begin with, so there was nothing you needed to do to remove them from your Git repo.

Note on -f

The -f / --force option means "this command will delete data, and I know what I'm doing." Before you type -f at any command prompt you should reflect for a moment to think about what data this command will delete and make sure that you actually want to delete it.

For example, git rm takes -f as a parameter. The git rm command will refuse to delete a file with uncommitted changes, because this will destroy the changes. Using -f overrides this behavior, allowing you to destroy data with git rm.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • 30
    I'm really, sincerely hoping this gets you a Reversal badge. – BoltClock Oct 23 '12 at 18:58
  • 3
    well I hope the OP does have some backups. Else I'd encourage the OP to take a look at some data [recovery questions](http://superuser.com/questions/tagged/data-recovery?sort=faq&pagesize=15) on [su] – Sathyajith Bhat Oct 23 '12 at 19:11
  • lol, just did my part to see that he gets that badge. – kayaker243 Oct 23 '12 at 21:00
  • Thanks for the answer, now I know what I've done there! Fortunately my system is fine for now, after a reboot, even I didn't recover my files(of course I don't have a backup), just saved the important files. Enough GIT for me for now! Have a nice day! – user1769195 Oct 23 '12 at 21:20
  • 4
    Stumbled across this linked in other threads. Just wanted to note there is all the more reason have backups if you save your repository on a more modern computer with SSD as SSDs actually immediately clean memory, unlike HDDs where you might be able to recover data if you're quick – Assimilater Jul 25 '17 at 19:09