我今天不小心在我的回购上运行了git reset --hard,而今天也有未提交的更改。为了恢复它,我运行git fsck --lost-found,它将所有未引用的blob写入<path to repo>/.git/lost-found/。由于文件未提交,我在<path to repo>/.git/lost-found/中的other目录中找到了它们。从那里,我可以使用git show <filename>查看未提交的文件,复制blob并重命名它们。
$ git reflog show
4b6cf8e (HEAD -> master, origin/master, origin/HEAD) HEAD@{0}: reset: moving to origin/master
295f07d HEAD@{1}: pull: Merge made by the 'recursive' strategy.
7c49ec7 HEAD@{2}: commit: restore dependencies to the User model
fa57f59 HEAD@{3}: commit: restore dependencies to the Profile model
3431936 HEAD@{4}: commit (amend): restore admin
033f5c0 HEAD@{5}: commit: restore admin
ecd2c1d HEAD@{6}: commit: re-enable settings app
# assuming you want to get back to 7c49ec7 (restore dependencies to the User model)
$ git reset HEAD@{2}
git reflog
git checkout <commit id> // now you are in where you want but you cannot push from detached branch to master
manually copy and paste changes from detached branch to master or working branch
git reset --hard HEAD // if needed
git add ... > git commit ... > git push ...
如果你使用的是(任何最新的)macOS,即使你不在时间机器磁盘上,操作系统也会保存每小时一次的备份,称为
# EYZ0。< / p >
进入时间机器,导航到你丢失的文件。操作系统会问你:
The location to which you're restoring "file.ext" already contains an
item with the same name. Do you want to replace it with the one you're
restoring?