1

Newbie needs some help undoing a mess!

I was tring to push my site to a git repository, so I used the git add command. Then, before I did the commit, I realized I had added everything from my root folder instead of just the directory I wanted. Since I didn't want to do that commit, I used the git rm --chached command to remove everything, thinking I was only changing what would be pushed to git. I also used git clean.

Now, I realize those commands actually deleted the files on my site! What have I done? If anyone can help me put things back in place, I would really appreciate it. -JB

Dabellator
  • 77
  • 1
  • 10
  • 1
    is there a previous commit you can revert to? if so, try `git reset --soft origin/master` – Amir Nissim Jun 08 '13 at 15:19
  • 1
    If you initilize a git repo, then add files, then remove them from the index with `git rm --cached` and then clean up with `git clean`, everything will be gone since at that point all your files are unknown to git, and it will remove them. (read the man page of `git-clean`) – steabert Jun 08 '13 at 15:33
  • Also, even though the file contents could probably be retrieved from the blobs (with e.g. `git fsck --lost-found`), I'm fairly sure the directory structure is gone. – steabert Jun 08 '13 at 15:44

3 Answers3

2

You can do

git reset --hard HEAD^

To move one step back in the version history, effectively undoing the last commit including all deletes.

If you have already pushed your changes you can instead do

git revert HEAD

To create a new commit reverting the delete commit, and then push that.

This answer assumes that you did commit the changes, and that was the last commit you did. If you didn't commit the changes, Willy's answer is better.

Klas Mellbourn
  • 42,571
  • 24
  • 140
  • 158
2

If you already deleted all files in your workspace. You can get it back by using the following command.

git checkout -f

The files should be back!

lvarayut
  • 13,963
  • 17
  • 63
  • 87
  • 1
    This assumes that the changes were never actually committed. It sounds to me like they were committed, but it is a bit unclear. – Klas Mellbourn Jun 08 '13 at 15:26
  • no, there was no commit, but it is also saying the branch is unborn. – Dabellator Jun 08 '13 at 15:33
  • @Dabellator `branch is unborn` sounds like you haven't done `git fetch` – Klas Mellbourn Jun 08 '13 at 15:35
  • Basically what it sounds like is that i used git clean to remove untracked files, and because of that they aren't coming back. Correct? – Dabellator Jun 08 '13 at 15:39
  • @Dabellator if you did git add then git rm --cached and then git clean on the same new files, with no commit in between, then they are hard to recover using Git. Maybe with `git fsck --lost-found` – Klas Mellbourn Jun 08 '13 at 16:06
0

have you tried git reset --hard (commit # before this horrible accident)?

then use git pull origin master to extract files from the online git repo to get your files back.

A more in depth explanation of this process is located on this SO thread.

Community
  • 1
  • 1
agconti
  • 17,780
  • 15
  • 80
  • 114