Dabellator
Dabellator

Reputation: 77

Undo deleted files in git?

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

Upvotes: 1

Views: 456

Answers (3)

Klas Mellbourn
Klas Mellbourn

Reputation: 44347

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.

Upvotes: 2

agconti
agconti

Reputation: 18093

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.

Upvotes: 0

lvarayut
lvarayut

Reputation: 15229

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!

Upvotes: 2

Related Questions