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.

13
  • 3
    You'll never get the untracked files back from git, since they aren't tracked they aren't recorded.
    – tpg2114
    Oct 23, 2012 at 18:36
  • 19
    Blindly typing commands will always end up here
    – sciritai
    Oct 23, 2012 at 18:40
  • 3
    I hope you have some recent backup of your computer...
    – Messa
    Oct 23, 2012 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, 2012 at 18:42
  • 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, 2012 at 18:52

1 Answer 1

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.

5
  • 30
    I'm really, sincerely hoping this gets you a Reversal badge.
    – BoltClock
    Oct 23, 2012 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 on Super User Oct 23, 2012 at 19:11
  • lol, just did my part to see that he gets that badge.
    – kayaker243
    Oct 23, 2012 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! Oct 23, 2012 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 Jul 25, 2017 at 19:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.