简体   繁体   English

Git恢复文件本地删除尚未提交

[英]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. 我是Git的新手,但不小心删除了桌面和库中的所有物理本地文件。 Due to initiating git from wrong directory. 由于从错误的目录启动git。 I have not committed it yet. 我还没有承诺。 How do I restore the files back ? 如何还原文件?

The current git status is : 当前的git状态是:

git status

On branch master Changes to be committed: (use "git reset HEAD ..." to unstage) 在分支主机上要提交的更改:(使用“ git reset HEAD ...”取消登台)

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) 现在:git status在分支主服务器上要落实的更改:(使用“ git reset HEAD ...”取消登台)

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. 物理删除的文件总数超过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. 如果您删除的文件没有被Git跟踪,您将无法使用Git找回它们。

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. 将来,尽早提交是个好主意,因为即使您提交得过于频繁,您也可以在将它们提交到公共存储库之前将它们合并在一起( squash )。

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). 编辑:根据您的评论,似乎在执行某些Git操作后,您丢失了已经由Git跟踪的文件(这意味着它们已添加到上一次提交中)。

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. 看起来您执行的操作使您进入“分离的HEAD”模式,这意味着您的HEAD(您在Git树中的位置)没有被分支指向。

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. 假设您在master分支上。 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. 然后执行git status并确保一切都与您执行的错误Git操作之前的外观相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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