简体   繁体   中英

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

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.

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!

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM