我用 Git 误删了大部分文件,有办法恢复吗?

问题描述 投票:0回答:1

我试图从命令行更新 GitHub 存储库,但现在我的大部分文件都不见了。我使用 VSCode 并打开包含我的项目的文件夹“2022 Portfolio”,然后从 VSCode 中的终端运行一些命令。

我关注了 YouTube 上的一个视频,使用命令行将我的代码上传到 GitHub。我遵循了我认为是通常的命令,我认为在我陷入麻烦之前我运行的最后一个命令是:

git push -u origin main To https://github.com/matthewlieb/2022-portfolio.git

这返回了以下

error: error: failed to push some refs to 'https://github.com/matthewlieb/2022-portfolio.git'

在此之后,我转向 ChatGPT 寻求帮助,我认为这是我遇到麻烦的地方。我有整个对话,但我在与聊天机器人来回交流后运行的命令(包括导致下一个命令的错误)如下:

git push origin main
git config pull.rebase false
git pull --allow-unrelated-histories origin main

现在,我的所有文件都丢失了。我不知道如何恢复备份或做什么。非常感谢任何帮助或建议,我可以根据需要提供任何其他信息。

git github backup restore
1个回答
1
投票

请不要开始使用这种方法进行木材加工:)

大家好消息!使用 git 真的很难丢失文件。作为第一个反应,转到

git reflog
:它会列出您在本地去过的所有先前阶段。 例子:

$ git reflog
341b480 (HEAD -> feature/49-uninstall-script, origin/feature/49-uninstall-script) HEAD@{0}: commit: fixup! fix authelia rsa command
353f80e HEAD@{1}: commit: fix authelia rsa command
3a79235 HEAD@{2}: commit: fixup! unrelated fix: sync authelia version
e951e1f HEAD@{3}: checkout: moving from develop to feature/49-uninstall-script
0e39f52 (origin/develop, origin/HEAD, develop) HEAD@{4}: checkout: moving from feature/49-uninstall-script to develop

从这个当地的历史中,找到一个看起来很有前途的并检查一下:

$ git checkout HEAD@{42}

从那里,您有一个处于适当状态的工作目录。现在是学习如何使用 git 将本地状态正确推送到原点的好时机。

© www.soinside.com 2019 - 2024. All rights reserved.