简体   繁体   中英

Git restore files locally deleted not committed yet

I'm new to Git and accidentally deleted all my physical local files on my desktop and library. Due to initiating git from wrong directory. I have not committed it yet. How do I restore the files back ?

The current git status is :

git status

On branch master Changes to be committed: (use "git reset HEAD ..." to unstage)

modified:   .DS_Store
new file:   .gitconfig
new file:   Desktop/.DS_Store

Now : git status On branch master Changes to be committed: (use "git reset HEAD ..." to unstage)

modified:   .DS_Store
new file:   .gitconfig
new file:   Desktop/.DS_Store

How do you get the local files back. The total files which have got deleted physically is more than 1.5gb. I don't want to commit them or push them to remote if they are in staging area. But I need to restore the files physically on the local desktop.

If the files you deleted had not been tracked by Git you will not be able to use Git to get them back.

This article talks about tracking files.

In the future, it's a good idea to commit early since even if you commit too frequently you can combine ( squash ) commits together before pushing them to a public repository.

EDIT: Per your comments it seems that after some Git operations you lost files that were already tracked by Git (meaning that they were added to a previous commit).

It also looks like the operation you performed put you in "detached HEAD" mode which means that your HEAD (where you are in the Git tree) is not pointed to by a branch.

So, probably it would be best to simply get back to the branch you were previously on. Let's say you were on the master branch. Run this command:

git checkout master

Then do a git status and make sure everything looks as it did prior to the faulty Git operations you performed.

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